skip to Main Content

I have a rails model Company and want to have pretty urls on it instead of the default id (for seo and security reasons). For most of the cases it works. I’ve noticed on Company names ending with a period, I receive an error like the one below:

No route matches [GET] "/admin/companies/Baking%20co."

To test this out, I removed the friendly_id implementation and simply overrided the to_param method

def to_param
  name
end

And changed the controller show action from

Company.find(params[:id]) 

to

Company.find_by_name(params[:id])

This has the same effect as the friendly_id method. I get most of the companies showing up but I still receive the same error on names ending with period.

2

Answers


  1. Per this StackOverflow page, did you try setting format: false in your routes.rb file?

    Login or Signup to reply.
  2. I think this StackOverflow page gives a better answer. The rails guides explain it in detail here.

    Basically, dynamic segments don’t accept dots so you have to explicitly allow them in your routes file with something like this:

    resources :companies, constraints: { id: /[^/]+/ }
    

    which allows anything but a slash, per the guides.

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