skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search