class ExampleController < ApplicationController layout 'my_layout' # 将会使用 app/views/layouts/my_layout.html.erb end
咱们必需要对用户提交的数据进行过滤html
def article_params #咱们只提取title,location,excerpt,body,published_at,其余的数据不用处理 params.require(:article).permit(:title, :location, :excerpt, :body, :published_at) end
<%= render 'header', :title => 'My Blog' %>
咱们在模板中能够这样子使用session
<% title %>
form_for([@article, @article.comments.new]) #至关与 form_for(:comment, @article.comments.new, url: [@article, @article.comments.new])
也能够用下面的方式app
form_for(:comment, @article.comments.new, url: article_comments_path(article_id: @article))
更简单直接的方法以下:
:控制器名,:url:xxx_path
less
例如咱们在ApplicationController
中有一个方法以下ui
def current_user return unless session[:user_id] @current_user ||= User.find_by_id(session[:user_id]) end
咱们就能够在ApplicationController
使用下面的方法,使得它能够在模板中使用helper_method :current_user
url