railstutorial 10.3.1:未定义的局部变量或方法`microposts_path',`micropost_path'

时间:2013-08-19 16:40:00

标签: ruby-on-rails-3 railstutorial.org

10.3.1 Access control中测试时遇到问题:

    Failures:

    1) Authentication authorization for non-signed-in users in the Microposts controller submitting to the destroy action 
 Failure/Error: before { delete micropost_path(FactoryGirl.create(:micropost)) }
 NoMethodError:
   undefined method `micropost_path' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_3::Nested_1::Nested_3::Nested_2:0x00000004edd970>
 # ./spec/requests/authentication_pages_spec.rb:117:in `block (6 levels) in <top (required)>'

    2) Authentication authorization for non-signed-in users in the Microposts controller submitting to the create action 
 Failure/Error: before { post microposts_path }
 NameError:
   undefined local variable or method `microposts_path' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_3::Nested_1::Nested_3::Nested_1:0x0000000521c758>
 # ./spec/requests/authentication_pages_spec.rb:112:in `block (6 levels) in <top (required)>'

我在routes.rb添加了这个:

 resources :misroposts, only: [:create, :destroy]

但它没有帮助。什么意思undefined method 'micropost_path'?我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

您的resources :microposts, only: [:create, :destroy]中有routes.rb行吗?查看Listing 10.22

这将创建两个命名路由,microposts_path(可通过POST请求访问)和micropost_path(可通过DELETE请求访问),您可以看到(如果您在控制台中输入rake routes,则应用您应用中的所有其他路径。

查看教程的Table 7.1 - 将resources :users添加到routes.rb时会发生什么。

Resources :microposts, only: [:create, :destroy]的工作方式相同,当然,除了Table 10.2中所述,您只会获得createdestroy次操作。