undefined local variable or method `root_path'

2020-03-01 07:58发布

问题:

So my first question on Stackoverflow..... =]

My tests in Chapter 5.3.2 of Michael Hartl's RoR Tutorial are all failing, when the tutorial says it should be passing (tutorial says 'about', 'contact', and 'help' should pass... but all of mine fail). Everything til now worked fine, but I changed the

get 'static_pages/help' 

to

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

so that my

config/routes.rb

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' 

and then everything went to hell. All my tests fails with a undefined local variable or method 'root_path' or 'about_path' etc. (see below, Terminal Output). But here are my relevant files... Just in case anyone is curious, all the files are exactly what Hartl has. I basically copied and pasted each file's content.

Can someone help me?????? Thanks!

spec/static_pages_spec.rb

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

Terminal output with input 'rspec spec/'

FFFFFFFFF

Failures:

  1) Static pages Contact page should have the h1 'Contact'
     Failure/Error: visit contact_path
     NameError:
       undefined local variable or method `contact_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x007fd2093855f8>
     # ./spec/requests/static_pages_spec.rb:55:in `block (3 levels) in <top (required)>'

  2) Static pages Contact page should have the title 'Contact'
     Failure/Error: visit contact_path
     NameError:
       undefined local variable or method `contact_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x007fd208cbeb20>
     # ./spec/requests/static_pages_spec.rb:60:in `block (3 levels) in <top (required)>'

  3) Static pages Help page should have the h1 'Help'
     Failure/Error: visit help_path
     NameError:
       undefined local variable or method `help_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fd2094c02b0>
     # ./spec/requests/static_pages_spec.rb:27:in `block (3 levels) in <top (required)>'

  4) Static pages Help page should have the title 'Help'
     Failure/Error: visit help_path
     NameError:
       undefined local variable or method `help_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fd209649208>
     # ./spec/requests/static_pages_spec.rb:32:in `block (3 levels) in <top (required)>'

  5) Static pages About page should have the h1 'About'
     Failure/Error: visit about_path
     NameError:
       undefined local variable or method `about_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3:0x007fd208f16d00>
     # ./spec/requests/static_pages_spec.rb:41:in `block (3 levels) in <top (required)>'

  6) Static pages About page should have the title 'About Us'
     Failure/Error: visit about_path
     NameError:
       undefined local variable or method `about_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3:0x007fd2094f7990>
     # ./spec/requests/static_pages_spec.rb:46:in `block (3 levels) in <top (required)>'

  7) Static pages Home page should not have a custom page title
     Failure/Error: visit root_path
     NameError:
       undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fd208d30b08>
     # ./spec/requests/static_pages_spec.rb:19:in `block (3 levels) in <top (required)>'

  8) Static pages Home page should have the base title
     Failure/Error: visit root_path
     NameError:
       undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fd208e0cb80>
     # ./spec/requests/static_pages_spec.rb:13:in `block (3 levels) in <top (required)>'

  9) Static pages Home page should have the h1 'Sample App'
     Failure/Error: visit root_path
     NameError:
       undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fd209607268>
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.30216 seconds
9 examples, 9 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:54 # Static pages Contact page should have the h1 'Contact'
rspec ./spec/requests/static_pages_spec.rb:59 # Static pages Contact page should have the title 'Contact'
rspec ./spec/requests/static_pages_spec.rb:26 # Static pages Help page should have the h1 'Help'
rspec ./spec/requests/static_pages_spec.rb:31 # Static pages Help page should have the title 'Help'
rspec ./spec/requests/static_pages_spec.rb:40 # Static pages About page should have the h1 'About'
rspec ./spec/requests/static_pages_spec.rb:45 # Static pages About page should have the title 'About Us'
rspec ./spec/requests/static_pages_spec.rb:18 # Static pages Home page should not have a custom page title
rspec ./spec/requests/static_pages_spec.rb:12 # Static pages Home page should have the base title
rspec ./spec/requests/static_pages_spec.rb:7 # Static pages Home page should have the h1 'Sample App'

回答1:

Mischa's answer will hopefully fix most of those problems, but your tests may still fail on the root_path issue.

Have you removed index.html like so: git rm public/index.html

And don't forget to commit your changes with: git commit -am "Message"



回答2:

I'll put this as an answer because the solution (from @mischa) is buried in the comments under @marflar's answer: try restarting Spork to solve this issue.



回答3:

I have same problem in this chapter, just change in config/routes.rb root to: 'static_pages#home' to root :to => 'static_pages#home' and it's start work. you can also check all routes with rake routes.



回答4:

In your Gemfile, change the rspec-rails line to: gem 'rspec-rails', '2.12.0'

This version of rspec supports the approach Hartl is using at that point in the book.



回答5:

  root :to => 'static_pages#home' 
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'

works for me

test results: 9 examples 0 failures

urls seem to have changed like this now:

http://localhost:3000/about


回答6:

I am using the same tutorial and ran into the exact same problem! You might've thought it was spork that fixed this but that wasn't it. The reason it worked is because you went ahead and upgraded the Rspec in the next section. It only works with the upgraded rspec - the old rspec was why your tests didn't pass. The site is wrong in that it tells users that the tests will pass before also changing the Rspec



回答7:

I had a similar issue. My fix was to include the following line in spec_helper.rb

config.infer_spec_type_from_file_location!

BTW gems are:

gem 'rails', '4.1.5'
gem 'rspec-rails', '~> 3.0'

and I also removed all the Guard and Spork stuff.