Rspec - nameError:未定义的局部变量或方法`sammiches_path'

时间:2013-04-28 22:20:01

标签: ruby-on-rails ruby rspec

我在这里有点困惑。我正在尝试做TDD并遇到了一个问题。 Rspec告诉我以下 -

1) Sammiches GET /sammiches display some sammiches
 Failure/Error: visit sammiches_path
 NameError:
   undefined local variable or method `sammiches_path' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1:0x007fa7afcfed70>
 # ./spec/requests/sammiches_spec.rb:7:in `block (3 levels) in <top (required)>'

2) Sammiches GET /sammiches creates a new sammich
 Failure/Error: visit sammiches_path
 NameError:
   undefined local variable or method `sammiches_path' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1:0x007fa7ae11e6f0>
 # ./spec/requests/sammiches_spec.rb:12:in `block (3 levels) in <top (required)>'

这是我的spec文件的样子

require 'spec_helper'

describe "Sammiches" do
describe "GET /sammiches" do
it "display some sammiches" do
    @sammich = Sammich.create :name => 'bacon'
    visit sammiches_path
    page.should have_content 'bacon'
end

it "creates a new sammich" do
    visit sammiches_path
    fill_in 'Sammich', :with => 'lechuga'
    click_button 'add Sammich'

    current_path.should = root_path
    page.should have_content 'lechuga'

    save_and_open_page
end
end
end

我的路线是 -

 sammiches_index GET    /sammiches/index(.:format)    sammiches#index
  Sammiches GET    /Sammiches(.:format)          Sammiches#index
            POST   /Sammiches(.:format)          Sammiches#create
 new_Sammich GET    /Sammiches/new(.:format)      Sammiches#new
 edit_Sammich GET    /Sammiches/:id/edit(.:format) Sammiches#edit
    Sammich GET    /Sammiches/:id(.:format)      Sammiches#show
            PUT    /Sammiches/:id(.:format)      Sammiches#update
            DELETE /Sammiches/:id(.:format)      Sammiches#destroy
       root        /                             Sammiches#index

路线 -

Sammiches::Application.routes.draw do
get "Sammiches/index"

resources :sammiches
 root :to => 'Sammiches#index'

新错误 -

1) Sammiches GET /sammiches creates a new sammich
 Failure/Error: visit sammiches_path
 ActionView::Template::Error:
   undefined method `sammich' for #<Sammich:0x007fd0007d2f80>
 # ./app/views/sammiches/index.html.erb:4:in `block in _app_views_sammiches_index_html_erb___2703584807867277870_70265660050820'
 # ./app/views/sammiches/index.html.erb:2:in `_app_views_sammiches_index_html_erb___2703584807867277870_70265660050820'
 # ./spec/requests/sammiches_spec.rb:12:in `block (3 levels) in <top (required)>'

2) Sammiches GET /sammiches display some sammiches
 Failure/Error: visit sammiches_path
 ActionView::Template::Error:
   undefined method `sammich' for #<Sammich:0x007fd00006f4f0>
 # ./app/views/sammiches/index.html.erb:4:in `block in _app_views_sammiches_index_html_erb___2703584807867277870_70265660050820'
 # ./app/views/sammiches/index.html.erb:2:in `_app_views_sammiches_index_html_erb___2703584807867277870_70265660050820'
 # ./spec/requests/sammiches_spec.rb:7:in `block (3 levels) in <top (required)>'

我在这里有点不知所措。任何建议都将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:2)

您似乎已在resources :Sammiches中添加了config/routes.rb

将其更改为resources :sammiches。坚持公约,有所帮助。 :)

您的路线文件应如下所示:

Sammiches::Application.routes.draw do

resources :sammiches
 root :to => 'sammiches#index'