Reputation: 9627
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?
Upvotes: 2
Views: 37
Reputation: 3723
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.
Upvotes: 0
Reputation: 1559
You change your url by creating a to_url method in your cars model.
def to_param
"#{name}" #=> bmw-x5-limited-edition
end
FYI: http://blog.teamtreehouse.com/creating-vanity-urls-in-rails
Upvotes: 1
Reputation: 446
define method in your car
model
def to_param
carname.to_s.gsub(/\s/, '-')
end
your URL would be http://mywebsite.com/cars/bmw-x5-limited-edition
but you need to find the record in the show
action by the carname
instead of id
because now you are sending carname
in place of id
.
Hope this helps.
Upvotes: 1