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)
I don’t think it is an hack what you are doing.
To actually have routes receiving different types of ids you probably needed to change how those helpers are generated by rails.
That means you might have to monkey patch rails.
The alternative rails is giving you to be able to do this is by doing this: admin_content_path(id: content.id)
So it allows you to pass to override the id passed as argument.
I am sorry but I dont think there is a way around this.
I have to say that Rails is great. 5.1 introduces a wonderful feature for my need: direct ActionDispatch::Routing::Mapper::CustomUrls
Now in config/routes.rb I can write:
Content model includes FriendlyId, so content_path(Content.first) calls record.to_param using the slug
resources :contents
direct ‘edit_admin_content’ do |record, options|
Here I specify to use the record id, so the resource url will fit better with admin section purposes
options.merge controller: ‘/admin/contents’, action: :edit, id: record.id
end
Same for actions :show, :update and :destroy. Problem solved!
Thats great!
I am glad you shared this.
You can also do something like this If i get it right,
resources :posts, param: :slug
resources :posts, param: :slug
FWIK this just changes the name of the parameter passed to the controller, so if you write post_path(Post.first) you will have a request like /posts/#{post.to_param} but in the controller instead of have param[:id] you have param[:slug]