1

I'm using the gem friendly_id in rails 4 to make nice urls for my user's product listings. It works by overriding the to_param. Now it is working wonderfully in the Shop namespace which is the public facing part of my site however in the Admin namespace I'd rather be using the regular ids because I don't need them there and I'd rather the urls be shorter.

I thought this would be easy but after digging it actually seems somewhat complicated. The to_param, because it's part of the model, has no real concept of what controller it is being called in. So the only option I see is to override url_for so it uses id in the Admin namespace. I'm not sure how to do that and if that really is the best coarse of action because messing with url_for seems a little dangerous.

hadees
  • 1,754
  • 2
  • 25
  • 36

2 Answers2

1

With FriendlyId5 finders are not overwritten by default.

friendly_id :foo, use: :slugged # you must do MyClass.friendly.find('bar')
# or...
friendly_id :foo, use: [:slugged, :finders] # you can now do MyClass.find('bar')

This way, I guess you can use the first option, and depending on the namespace call MyClass.friendly.find('params[:id]') or MyClass.find('params[:id]')

nunopolonia
  • 14,157
  • 3
  • 26
  • 29
-1

You might want to use friendly_id in case if you do not want to mess with to_params. It is easier to implement to make excellent friendly urls that are both machine and human friendly http://kapilrajnakhwa.com/blogs/user-friendly-urls-with-friendly_id-gem-in-rails-4

xecutioner
  • 311
  • 3
  • 15