自定义辅助方法
定义 full_title 辅助方法,编辑 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,替换 title
<title><%= full_title(yield(:title)) %></title>
把 home.html.erb 的 title 删除
编辑 test/controllers/static_pages_controller_test.rb,修改测试代码
require "test_helper"
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
@base_title = "Ruby on Rails Tutorial Sample App"
end
test "should get root" do
get static_pages_home_url
assert_response :success
assert_select "title", "#{@base_title}"
end
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "#{@base_title}"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | #{@base_title}"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | #{@base_title}"
end
test "should get contact" do
get static_pages_contact_url
assert_response :success
assert_select "title", "Contact | #{@base_title}"
end
end
bin/rails test
测试成功!