0

I keep getting No route matches error for a nested resource #create action. Here is what I got:

routes:

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

controller spec:

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

error:

No route matches {:action=>"create", :controller=>"filters", :filter=>{[long filter hash]}
dimitry_n
  • 2,939
  • 1
  • 30
  • 53

1 Answers1

0

Because it is nested, you also need to include the ID for the parent object that the filter will belong to.

post :create, user_id: <some_user_id>, {:filter => attributes_for(:filter)}
Alexa Y
  • 1,854
  • 1
  • 10
  • 13
  • `unexpected keyword_end, expecting '}'` syntax error. I tried passing user id in the options hash like this: `post :create, {:filter => attributes_for(:filter), :user_id => 1}`, and got a different error: `undefined method "filters" for "1":String` – dimitry_n Feb 27 '15 at 20:27
  • That is true. Using multiple hash types tends to confuse ruby. Something like this would probably do better. `post :create, user_id: u, filter: attributes_for(:filter)` – Alexa Y Feb 27 '15 at 20:44
  • same thing. `user_id: u` returns `undefined method / variable "u"`. I tried declaring user id through an instance variable `@user`, `:post :create, user_id: @user.id, filter: attributes_for(:filter)` and got the routing error again. – dimitry_n Feb 27 '15 at 21:06