neo
neo

Reputation: 4116

Rails routes formatted params in URL

I have the following in my routes:

match '/car-rentals(/:address)' => 'search#search', via: :get

I can visit:

localhost:3000/car-rentals/montreal

Shows all available cars for Montreal.

When I enter: localhost:3000/car-rentals/Montreal Quebec

My URL becomes localhost:3000/car-rentals/Montreal%20quebec

I would like it to be: localhost:3000/car-rentals/montreal-quebec

I tried using to_param in my model.

class Search < ActiveRecord::Base
  def to_param
    "#{address.parameterize}"
  end
end

Any other ways to achieve this?

Also worth mentioning. When I stick binding.pry in to_param and reload the page, it doesn't stop the process, so not getting there.

  def to_param
    binding.pry # on page load, not getting here
    "#{address.parameterize}"
  end

Thanks in advance!

Upvotes: 0

Views: 54

Answers (1)

Lyra
Lyra

Reputation: 11

There are several gems that generate slugs for SEO friendly urls.

I've used https://github.com/norman/friendly_id in the past and it worked fine. It also has very helpful documentation.

There is also this: https://github.com/Sutto/slugged

Upvotes: 1

Related Questions