您的位置:首页 > 编程语言 > Ruby

第四章 Rails背后的Ruby

2013-12-04 18:19 363 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: