4

I have the following nested singular route:

resources :listings do
  resource :logo, only: [ :edit, :update ]
end

It generates 2 correct routes as expected:

edit_listing_logo GET /listings/:listing_id/logo/edit(.:format) logos#edit
listing_logo PUT /listings/:listing_id/logo(.:format) logos#update

Now when I redirect to edit_listing_logo_path

redirect_to edit_listing_logo_path( @listing, @logo )

or when I create an update form

<%= form_for [ @listing, @logo ] do |f| %>

the resulting link has always singular resource ID attached at the end like this

/listings/2/logo.1

I'm not using respond formats in this app yet so it's working fine. But this link generation seems strange and I expect it to cause problems if used with various respond formats.

Note, that this was also discussed here Rails Nested Singular Resource Routing but I'm not using 'show' path at all.

Thanks for any inputs.

Community
  • 1
  • 1
Martin Sojka
  • 523
  • 5
  • 18
  • 'edit' and 'show' path work the same way so you've got everything you need in the question you mentioned ! – siekfried Jan 21 '13 at 13:33
  • why would they work the same way? – Martin Sojka Jan 21 '13 at 13:54
  • 1
    Sorry about that, it's just for the first part of your answer : you can use `redirect_to edit_listing_logo_path(@listing)` because if you check this line : `edit_listing_logo GET /listings/:listing_id/logo/edit(.:format) logos#edit`, you can see that :logo_id is not required in the route. For the second part, you can add a show path and use @Bryan Marble answer. – siekfried Jan 21 '13 at 14:06
  • Okay.. in redirects, this works `edit_listing_logo_path( @listing )` and make sense since we don't need ID of the logo. – Martin Sojka Jan 21 '13 at 14:34
  • But I had to change the form helper code to `form_for @logo, url: listing_logo_path( @listing ), method: :put`. This generates correct /listings/2/logo PUT action. IMO `form_for [ @listing, @logo ] do |f|` should generate same link as well so I think it's a bug. Removing @listing or @logo doesn't help in this case because it tries to generate not-nested route in both cases. – Martin Sojka Jan 21 '13 at 14:39
  • @MartinSojka Any update on this, I agree form_for [ listing, logo ] do |f| should work – Greg Dean Jul 03 '13 at 15:56

1 Answers1

1

I believe you can get the answer you need either by using the verbose path method edit_listing_logo_path(@listing) (as mentioned by @Martin Sojka) or by using a symbol instead of the object itself.

So instead of using the normal form:

form_for [ @listing, @logo ]

you can dispose of the object specificity (and the trailing ID) by writing:

form_for [@listing :logo]

and if you want to call another action on the controller you can prefix it in the array:

form_for [:edit, @listing :logo]

Hope it works for you - it seemed to solve my issues.

Peter Nixey
  • 16,187
  • 14
  • 79
  • 133
  • Yes it's using the id of the 2nd object and appending it as the format of the request URL. The object id is unnecessary as it is a singular resource. – Chloe Feb 28 '18 at 19:51
  • I think you're missing a `,` in your 2nd & 3rd examples. Also, it doesn't seem to work with or without the comma in Rails 5. – Ritchie Jul 04 '19 at 09:24