I am having a product controller which allows me to access the show method in the usual rails way
product GET /products/:id(.:format) products#show
Now, I would like to have a unique hash url for each product, which can be accessed at the root path e.g.
mydomain.com/24fe3455g
What would be the best way to do this?
Best
Philip
First of all, you need to define a matching route in your
routes.rb. Since it kind of a “fall through” you would usually put it at the end of the routes. have a look at the guides for how to define that: http://guides.rubyonrails.org/routing.htmlif you want to always use this hash-style urls throughout your application instead of ids, you would also implement
to_paramandself.from_parammethods in your model: http://apidock.com/rails/ActiveRecord/Base/to_paramyou could also use a gem like friendly_id if you like some tool support: http://rubydoc.info/github/norman/friendly_id/master/file/Guide.rdoc