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