【问题标题】:undefined local variable or method `root_path' Hartl's Tutorial Chapter 5.3.2未定义的局部变量或方法 `root_path' Hartl's Tutorial Chapter 5.3.2
【发布时间】:2012-09-03 00:44:16
【问题描述】:

所以我关于 Stackoverflow 的第一个问题..... =]

我在Michael Hartl's RoR Tutorial 的第 5.3.2 章中的测试都失败了,当教程说它应该通过时(教程说“关于”、“联系”和“帮助”应该通过......但我的全部失败)。到目前为止一切正常,但我改变了

get 'static_pages/help' 

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

这样我的

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' 

然后一切都陷入了地狱。我所有的测试都因未定义的局部变量或方法“root_path”或“about_path”等而失败(见下文,终端输出)。但这里是我的相关文件……以防万一有人好奇,所有文件都是 Hartl 所拥有的。我基本上复制并粘贴了每个文件的内容。

谁能帮帮我??????谢谢!

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

带有输入'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'

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 rspec


【解决方案1】:

我会将此作为答案,因为解决方案(来自@mischa)隐藏在comments under @marflar's answer: try restarting Spork to solve this issue 中。

【讨论】:

  • 这是可接受的答案。至少,将链接返回到给出正确答案的评论。
  • 这是此页面上已接受答案的第十条评论。我可以链接到评论吗?我最初的困惑是因为当我进入页面时,对我有用的答案的评论被隐藏了。
  • Yes, you can link to comments,但平心而论,新用户在哪里可以找到 URI 可能并不完全清楚。评论末尾的发帖时间(如May 4 at 15:45代表mine above)就是链接。
  • 谢谢 Jerry -- 可能也应该搜索一下。
【解决方案2】:

Mischa 的回答有望解决大部分问题,但您的测试可能仍会在 root_path 问题上失败。

你有没有像这样删除 index.html:git rm public/index.html

别忘了提交您的更改:git commit -am "Message"

【讨论】:

  • github 代码:github.com/sambaek/sample_app 我刚刚完成了教程的其余章节,但代码仍然抛出完全相同的错误...谢谢大家!
  • 我认为您应该删除其中一个 Rspec.configure 块,只保留 spork prerun 块内的那个 - ps 我现在正在运行您的测试,将报告我的发现跨度>
  • 哦,是的,我明白了。对不起。你在用Spork吗?您是否尝试重新启动 Spork 服务器?
  • omg... 这是一个 spork 问题。太感谢了!我真的很感谢你们的帮助..
  • 已解决...谢谢!没有你们,我会一直卡住,直到我重新启动我的电脑或什么哈哈。再次感谢
【解决方案3】:

我在本章中遇到了同样的问题,只需将 config/routes.rb root 更改为:'static_pages#home' to root :to => 'static_pages#home' 开始工作了。您还可以使用 rake routes 检查所有路线。

【讨论】:

  • 实际上因为我使用的是 ruby​​ 1.9.3,所以哈希语法不应该影响它。 to: somethingcheck:to =&gt; something 相同。检查您使用的是哪个版本的 ruby​​...如果是 1.8.7,那么您需要使用您的方法
【解决方案4】:

在您的 Gemfile 中,将 rspec-rails 行更改为: gem 'rspec-rails', '2.12.0'

这个版本的 rspec 支持 Hartl 在书中使用的方法。

【讨论】:

    【解决方案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'
    

    为我工作

    测试结果:9 例 0 次失败

    url 现在好像变了这样:

    http://localhost:3000/about
    

    【讨论】:

      【解决方案6】:

      我正在使用相同的教程并遇到了完全相同的问题!您可能认为是 spork 解决了这个问题,但事实并非如此。它起作用的原因是您在下一节中继续升级了 Rspec。它仅适用于升级后的 rspec - 旧的 rspec 是您的测试未通过的原因。该网站是错误的,因为它告诉用户测试将在更改 Rspec 之前通过

      【讨论】:

      • 你用的是什么版本的 rspec?
      【解决方案7】:

      我遇到了类似的问题。我的解决方法是在 spec_helper.rb 中包含以下行

      config.infer_spec_type_from_file_location!
      

      顺便说一句,宝石是:

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

      我还删除了所有 Guard 和 Spork 的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        相关资源
        最近更新 更多