使用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
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></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' %>
</head>
.
.
.
</html>
/app/views/layouts/about.html.erb
<% provide(:title, "About") %> //传递的参数
<h1>About</h1>
<p>
The <a href="http://www.railstutorial.org/"><em>Ruby on Rails
Tutorial</em></a> is a
<a href="http://www.railstutorial.org/book">book</a> and
<a href="http://screencasts.railstutorial.org/">screencast series</a>
to teach web development with
<a href="http://rubyonrails.org/">Ruby on Rails</a>.
This is the sample application for the tutorial.
</p>
/app/views/layouts/home.html.erb
<% provide(:title, "Contact") %>
<h1>Contact</h1>
<p>
Contact the Ruby on Rails Tutorial about the sample app at the
<a href="http://www.railstutorial.org/contact">contact page</a>.
</p>
<% provide(:title, 'xxxx') %>
可以用yield(:title)
接收到。