Matthew Berman
Matthew Berman

Reputation: 8631

Nested routes causing issues with my forms

I have my nested route setup as follows:

  resources :clients do
    resources :contacts
  end

and my update action form as so:

= form_for @contact, :url => client_contacts_path(@contact.client), :html => { :multipart => true } do |f|

however when I go to save I get this error:

No route matches [PUT] "/clients/3/contacts"

I'm not quite sure why it isn't trying the route: /clients/3/contacts/:id

what am i missing from my form?

Upvotes: 0

Views: 38

Answers (1)

Kocur4d
Kocur4d

Reputation: 6931

Try to change your form_for call to:

= form_for [@client, @contact], :html => { :multipart => true } do |f|

Rails will figure out all it need by it self:

  • when @contact is a new record it will generate /clients/:client_id/contacts/new url
  • when @contact already exist it will generate /clients/:client_id/contacts/:id/edit url

More info about form_for.

Upvotes: 2

Related Questions