Ruby on Rails Tuesday, March 21, 2017

Hello,

I have never found a good solution for the following problem. I have the usual structure:

resources :contents

namespace :admin do
  resources :contents
end

When I call content_path(content) I want the id to be the slug of the content, while when I call admin_content_path(content) I want the id to be the id of the content. I just want the id not to be related on the model (actually the id is the returning value of the to_param method of the model), but on the route.

I know I can write admin_content_path(id: content.id) or content_path(id: content.slug), but this is just an hack actually. Also, this is especially annoying in form_for, since I can't write

form_for @content

but I'm forced to use

form_for @content, url: @content.new_record? ? admin_contents_path : admin_contents_path(id: @content.id)


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b76a4010-1cd8-432c-ac7b-7cf508cb7c33%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment