对于嵌套资源控制器规范 - Rails,Rspec,“无路由匹配”错误

时间:2015-02-27 20:07:07

标签: ruby-on-rails ruby rspec nested-routes

我为嵌套资源No route matches操作一直收到#create错误。这是我得到的:

路线:

...
resources :users, only: [:show, :create] do 
  resources :filters, only: [:new,:create]
end
...

控制器规范:

...
context 'with valid attributes' do 
  it "creates new Filter" do
    expect{
    post :create, {:filter => attributes_for(:filter)}
    }.to change(Filter, :count).by(1)
  end
...

错误:

No route matches {:action=>"create", :controller=>"filters", :filter=>{[long filter hash]}

1 个答案:

答案 0 :(得分:0)

因为它是嵌套的,所以还需要包含过滤器所属的父对象的ID。

post :create, user_id: <some_user_id>, {:filter => attributes_for(:filter)}