> Problem : there's no easy solution to achieve this, because overriding to_param is global.
Have you considered wrapping the object in a decorator that changes the `to_param` method to what you want? url_for(@article) # => /articles/2019-05-my-breaking-news url_for(AdminArticleDecorator.new(@article)) # => /admin/articles/12 class AdminArticleDecorator def initialize(article) @artile = article end def to_param(*) @article.id end end Rafael Mendonça França http://twitter.com/rafaelfranca https://github.com/rafaelfranca On Thu, May 9, 2019 at 7:29 PM Romain Goyet <romain.go...@numworks.com> wrote: > Hi everyone! > > I've been using rails for a while (since rails 2) but never had the > opportunity to contribute. I'd like to take this opportunity to thank all > contributors to Rails :) > > Anyway, I'm running into some issue with to_param. Consider the case where > you have a resource that you'd like to route using identifiers that are > *not* the :id. Currently, the recommended way to achieve that is by > overriding the #to_param method on the model. I think that's questionable > design. > > Let's consider a simple example : you want to expose the same resource at > two different routes. For instance, on a blog, you'd want to route articles > using a slug for SEO on public URLs, and using an id on the admin interface > because you use like to use ids internally. > > /articles/2019-05-my-breaking-news > /admin/articles/12 > > Problem : there's no easy solution to achieve this, because overriding > to_param is global. > > Suggested solution : that's actually a routing topic, so it should be > solve in the routing. I suggest leveraging the "param" routing option to do > this, and leave the Models alone :) > > routes.draw do > resources :articles, param: :slug > namespace :admin do > resources :articles > end > end > > Currently, this changes the name of the parameter used in the route. > That's a good start. But it has *no* impact on the named route helpers. > Namely, url_for(@article) will still use the :id, even though ":slug" has > been specified. > I suggest to also change the named route helpers to call > "@article.#{param}" if a routing param has been specified. > > I hope this suggestion makes sense, just let me know :-) > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-core+unsubscr...@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at https://groups.google.com/group/rubyonrails-core. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-core/eac44ecb-df8d-45f2-ae8d-cb1ad04f94a1%40googlegroups.com > <https://groups.google.com/d/msgid/rubyonrails-core/eac44ecb-df8d-45f2-ae8d-cb1ad04f94a1%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at https://groups.google.com/group/rubyonrails-core. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/CAC9YFzd-BbECWXs3wQfRCA_k3vW4X9KtgjEW3kFjamUFAShLTw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.