1

I've been following along in the tutorial exactly as it has been written. So far everything has gone without a hitch, until this section.

I was supposed to change the "GET" statements in the config/routes.rb file to the following:

SampleApp::Application.routes.draw do
  root to: 'static_pages#home'

  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'

This was supposed to make the tests pass. They do not pass. They continue to fail with the following error as one of the 9 similar errors:

Failure/Error: visit about_path
NameError:
   undefined local variable or method 'about_path' ....

I have no idea how to get this to pass so that I can move on. What did I miss? What did Hartl miss? Other people who have asked this question never got an answer that made any sense or even worked when tried.

Before anyone asks:

All versions of Rails, Ruby and other installed components are the exact same versions used in the tutorial as it is written today 2012-10-05. Everything matches the tutorial perfectly.

UPDATE: Here is the current static_pages_spec.rb file

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the h1 'Sample App'" do
      visit root_path
      page.should have_selector('h1', text: 'Sample App')
    end

    it "should have the base title" do
      visit root_path
      page.should have_selector('title',
                        text: "Ruby on Rails Tutorial Sample App")
    end

    it "should not have a custom page title" do
      visit root_path
      page.should_not have_selector('title', text: '| Home')
    end
  end

  describe "Help page" do

    it "should have the h1 'Help'" do
      visit help_path
      page.should have_selector('h1', text: 'Help')
    end

    it "should have the title 'Help'" do
      visit help_path
      page.should have_selector('title',
                        text: "Ruby on Rails Tutorial Sample App | Help")
    end
  end

  describe "About page" do

    it "should have the h1 'About'" do
      visit about_path
      page.should have_selector('h1', text: 'About Us')
    end

    it "should have the title 'About Us'" do
      visit about_path
      page.should have_selector('title',
                    text: "Ruby on Rails Tutorial Sample App | About Us")
    end
  end

  describe "Contact page" do

    it "should have the h1 'Contact'" do
      visit contact_path
      page.should have_selector('h1', text: 'Contact')
    end

    it "should have the title 'Contact'" do
      visit contact_path
      page.should have_selector('title',
                    text: "Ruby on Rails Tutorial Sample App | Contact")
    end
  end
end

Rake Routes results:

   root  /                  static_pages#home
   help  /help(.:format)    static_pages#help
   about  /about(.:format)   static_pages#about
   contact  /contact(.:format) static_pages#contact
Nathan McKaskle
  • 2,926
  • 12
  • 55
  • 93

6 Answers6

3

Try changing in your routes.rb your line to:

match '/about',   to: 'static_pages#about', as: 'about'

and also you should reload Spork in order to changes apply. You can also add:

load "#{Rails.root}/config/routes.rb"

into your Spork.each_run block in order to reload routes each time you run Spork.

Vadym Chumel
  • 1,766
  • 13
  • 20
  • No such luck, still fails. I think he (Hartl) must have forgotten something in his tutorial. – Nathan McKaskle Oct 05 '12 at 19:09
  • @Sephethus do you have `require 'spec_helper'` in your spec file? Add it if you haven't. – Vadym Chumel Oct 05 '12 at 19:16
  • Yes, see contents above in the update edit to my question. Thanks. – Nathan McKaskle Oct 05 '12 at 19:22
  • 2
    @Sephethus I think you should reload Spork after routes.rb changes. Did you done that? – Vadym Chumel Oct 05 '12 at 19:30
  • No I hadn't done that. It got zero failures when I did. I don't get why I should have to reload it, kind of defeats the purpose of guard w/spork. You should edit your answer to say reload spork. – Nathan McKaskle Oct 05 '12 at 19:38
  • @Sephethus done! Also added solution for routes reloading without restarting Spork. – Vadym Chumel Oct 06 '12 at 13:53
  • In case anyone makes it this far and is still stumped, it's also worth noting that root_path (etc) only works within the "do...end" block of an "it" section within a "describe" block of the RSpec tests. So if you created functions to handle "it" sections based on a path parameter passed to the function, you can't pass root_path (etc) as a parameter to said function. – DreadPirateShawn Jan 30 '13 at 07:40
1

Reloading spork worked for me.

user424703
  • 251
  • 1
  • 2
  • 4
0

Hi try to add this on your spec.

describe "Static pages" do
include Rails.application.routes.url_helpers
.......
hey
  • 90
  • 10
0

I think you should type in the browser

http://localhost:3000/

instead of http://localhost:3000/static_pages/home to get the home page for example. I think the problem is not the code but rather how to access the page. In the sections prior to 5.3.2 in the tutorial, you needed to visit localhost:3000/static_pages/home to access the homepage. After you follow instructions in 5.3.2, you only need to type http://localhost:3000/.

christegho
  • 184
  • 12
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – SteveTurczyn Jul 19 '14 at 21:33
  • Thanks for the clarification. However, I wrote that post because I got the same problem as Stephetus and figured out that the problem was not the code but rather how to access the page. In the sections prior to 5.3.2 in the tutorial, you needed to visit http://localhost:3000/static_pages/home to access the homepage. – christegho Jul 20 '14 at 03:04
  • That's fine but that was unclear in your answer.... refer to the page where the reference is explicit and point out that not following that advice causes the problem. As you've posted it, it just looks like a non-sequitor. – SteveTurczyn Jul 20 '14 at 09:20
0

I just encountered this error, and had some help fixing this. I had mistakenly added the signin_path and signup_path on the layouts/_header.html.erb and static_pages/home.html.erb, respectively. This had EVERY page confused as to what the signin_path was.

The other issue I had made an error on was the root path. If you run 'rm public/index.html' from your sample app directory, 'root 'static_pages#home' in routes.rb, should work. The problem is that things in the public directory override any other part of your app as you customize it. Hope this helps you like it helped me :)

0

Named routes are not available in specs by default. Add the following code to the spec_helper.rb file:

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
end
Roan
  • 1,200
  • 2
  • 19
  • 32