作者迈克尔·哈特尔说:
这里的规则:
get "static_pages/home"
将对 URI /static_pages/home 的请求映射到 StaticPages 控制器中的 home 操作。
如何?给出了请求的类型,给出了 url,但是控制器和动作的映射在哪里呢?不过,我的测试都通过了。
我还尝试删除 StaticPagesController 中的所有操作,如下所示:
class StaticPagesController < ApplicationController
def home
end
def about
end
def help
end
def contact
end
end
...而且我的测试仍然通过,这令人费解。不,我删除了所有这样的操作:
class StaticPagesController < ApplicationController
end
这本书的第二版(在线)真的很令人沮丧。具体来说,无法遵循有关更改 Guardfile 的部分。例如,如果我指示您编辑此文件:
blah blah blah
dog dog dog
beetle beetle beetle
jump jump jump
并进行以下更改:
blah blah blah
.
.
.
go go go
.
.
.
jump jump jump
...您知道代码中的“go go go”行应该在哪里吗?
练习 3.5-1 的提示完全错误。如果作者在每章的末尾都放置评论部分,那么 Rails 社区就可以自行编辑这本书。
测试:
require 'spec_helper'
describe "StaticPages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the title 'Home'" do
visit "/static_pages/home"
page.should have_selector(
'title',
:text => "#{base_title} | Home")
end
end
describe 'Help page' do
it "should have the h1 'Help'" do
visit "/static_pages/help"
page.should have_selector('h1', :text => 'Help')
end
it "should have the title 'Help'" do
visit '/static_pages/help'
page.should have_selector(
'title',
:text => "#{base_title} | Help")
end
end
describe 'About page' do
it "should have the h1 'About'" do
visit '/static_pages/about'
page.should have_selector('h1', :text => 'About')
end
it "should have the title 'About'" do
visit '/static_pages/about'
page.should have_selector(
'title',
:text => "#{base_title} | About")
end
end
describe 'Contact page' do
it "should have the h1 'Contact'" do
visit '/static_pages/contact'
page.should have_selector('h1', :text => 'Contact')
end
it "should have the title 'Contact'" do
visit '/static_pages/contact'
page.should have_selector(
'title',
:text => "#{base_title} | Contact")
end
end
end