Seoman
Seoman

Reputation: 783

Rails 3.2 nested route on simple form

I have a nested route between object1 and object2. When I try to edit an object2 I got an error:

undefined method `object2_path' for #<#Class:0x000000040b2fa8>:0x000000029c8810>.

config/routes.rb

resources :object1 do
  resources :object2
end

view/object2/_form.html.haml:

= simple_form_for [@object1, @object2] do |f|

If I change the view to add a specific url like:

= simple_form_for [@object1, @object2], :url => object1_object2_path, do |f|

then the edit works but new doesn't.

If instead of object1_object2_path as url I set object1_object2s_path (the index path), both views are rendered but edit fails because the form is pointing to the wrong url (that's obvious, is just a part of the tries I did).

Upvotes: 3

Views: 4004

Answers (1)

Mik
Mik

Reputation: 4187

It seems @object1 is nil in this case:

simple_form_for [@object1, @object2] do |f|

So rails attempts to use the object2_path instead of object1_object2_path. Try to check the value of @object1.

Upvotes: 13

Related Questions