第四章 Rails背后的Ruby
4.1 导言
定义full_title帮助方法
$ vim 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 "#{base_title} | #{page_title}" end end end
简化布局
$ vim app/views/layouts/application.html.erb
修改title
<title><%=full_title(:title)%></title>
更新现有的测试:
vim spec/requests/static_pages_spec.rb
require 'spec_helper' describe "Static pages" do describe "Home page" do it "should have the content 'Sample App'" do visit '/static_pages/home' expect(page).to have_content('Sample App') end it "should have the base title" do visit '/static_pages/home' expect(page).to have_title("Ruby on Rails Tutorial Sample App") end it "should not have a custom page title" do visit '/static_pages/home' expect(page).not_to have_title("| Home") end . . . end
运行测试,获得一个测试失败。
$ bundle exec rspec spec/requests/static_pages_spec.rb
为了让测试通过删除首页视图中的provide那行
4.2 字符串和方法
进入控制台
$rails console

浙公网安备 33010602011771号