helper方法

使用helper方法
/app/helpers/application_helper.rb

module ApplicationHelper
    #根据页面返回相应的标题
    def full_title(page_title="")
        base_title = "Ruby on Rails Tutorial Sample App"
        if page_title.empty?
            base_title
        else
            page_title + ' | ' + base_title
        end
    end
end

/app/views/layouts/application.html.erb



  
    <%= full_title(yield(:title)) %>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
    <%= render 'layouts/shim' %>
  
.
.
.

/app/views/layouts/about.html.erb

<% provide(:title, "About") %>    //传递的参数

About

The Ruby on Rails Tutorial is a book and screencast series to teach web development with Ruby on Rails. This is the sample application for the tutorial.

/app/views/layouts/home.html.erb

<% provide(:title, "Contact") %>

Contact

Contact the Ruby on Rails Tutorial about the sample app at the contact page.

<% provide(:title, 'xxxx') %> 可以用yield(:title)接收到。

你可能感兴趣的:(helper方法)