Rails Vanity URLs for multiple models

445 Views Asked by At

I need to setup vanity urls for some models in my application, basically it's a product catalogue and I have Store, Brand and Category models. What I need here is setting up username like URLs for all these 3 models, so the routes should appear like

http://mysite.com/brandname => http://mysite.com/dell
http://mysite.com/categoryname => http://mysite.com/laptops
http://mysite.com/storename => http://mysite.com/delluk

All these 3 routes should render index action of products controller and show the products belongs for the given store, brand or category.

Please let me know if there is gem to get this done. I'm trying this gem https://github.com/jaustinhughey/vanities at the moment but yet I couldn't figure out how to use it as described above.

1

There are 1 best solutions below

0
On BEST ANSWER

That gem you posted looks like it would work for you. Basically you would follow a process like this to use it:

brand = Brand.find_by__name("Dell")
brand.vanity = Vanity.new(:name => "dell_laptops")

However it looks like with that gem you are limited to only one vanity per record which seems a bit restrictive.

I would take a look at FriendlyId it's designed to automatically provide good URL's that are easy to remember and SEO friendly. There is even a RailsCast on FriendlyId that shows you the ropes so if you get stuck the video can help you along. Best of luck with your application, let me know if you have any questions.