Ruby on Rails Tuesday, March 21, 2017


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

resources :contents

namespace :admin do
  resources :contents

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: 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:

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
To post to this group, send email to
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment