I am creating simple e-commerce website.
I don’t want to use spree gem cause it has some problems with localization.
So I decided to build my own.
I will have some categories which i will take from database.
Let’s say I take category cars
And there I click on bmw x5 limited edition
So I want to make browser address string look like
http://mywebsite.com/cars/bmw-x5-limited-edition
I need this for seo. I don’t want to use get params like mywebsite.com/cars/5?carname=bmw-x5
Also there will be many categories and products which will be added to database through admin dashboard.
So how do I do this?
3
Answers
define method in your
car
modelyour URL would be
http://mywebsite.com/cars/bmw-x5-limited-edition
but you need to find the record in the
show
action by thecarname
instead ofid
because now you are sendingcarname
in place ofid
.Hope this helps.
You change your url by creating a to_url method in your cars model.
FYI: http://blog.teamtreehouse.com/creating-vanity-urls-in-rails
This gem may also help you: https://rubygems.org/gems/sanetitle. It does exactly what you want, removing special chars and putting everything in lowercase. You may also limit the number of chars in the returned value.