The author, Michael Hartl, says:
Here the rule:
get "static_pages/home"maps requests for the URI /static_pages/home to the home action in the StaticPages controller.
How? The type of request is given, the url is given, but where is the mapping to a controller and action? My tests all pass, though.
I also tried deleting all the actions in the StaticPagesController, which just looks like this:
class StaticPagesController < ApplicationController
def home
end
def about
end
def help
end
def contact
end
end
…and my tests still pass, which is puzzling. No, I deleted all my actions like this:
class StaticPagesController < ApplicationController
end
The 2nd edition of the book(online) is really frustrating. Specifically, the section about making changes to the Guardfile is impossible to follow. For instance, if I instruct you to edit this file:
blah blah blah
dog dog dog
beetle beetle beetle
jump jump jump
and make these changes:
blah blah blah
.
.
.
go go go
.
.
.
jump jump jump
…would you have any idea where the line ‘go go go’ should be in the code?
And the hint for exercise 3.5-1 is flat out wrong. If the author would put up a comment section at the end of every chapter, the rails community could self-edit the book.
Tests:
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
I found an answer to this conundrum. First of all, this is a rails ‘problem’ not an rspec problem; if I add a route to routes.rb such as:
…and then enter the url
in my browser, rails complains:
Then if I add the dog action to the controller, then create a view,
everything works fine and dandy.
But if I then delete the dog action, and then use the same url,
in my browser, this time I get a different result–instead of getting an error the view displays.
As it turns out, that inconsistent behavior is a rails ‘feature’ called default rendering, explained here:
http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-by-default-convention-over-configuration-in-action
According to the docs, all that’s needed to render a page is a route and a view–the action is optional.