8

I have overwritten to_param method in my Category model

def to_param
  name
end

And routes.rb

get '/:id' => 'categories#show', :as => :category

When name parameter doesn't contain any dots (foobar), all works right, but when it does (f.o.o.b.a.r) I get an error No route matches [GET]. So my question is: is it possible to use dots in routing like a part of name of parameter? Or what can I do to accomplish this purpose, maybe some hooks or something. Any help is appreciated.

evfwcqcg
  • 13,825
  • 14
  • 51
  • 69

1 Answers1

15

You can change the constraints for this route:

get ':/id' => "categories#show", :as => :category, :constraints => { :id => /[\w+\.]+/ }

This route will now match :id to any string containing any word character or a dot.

Ryan Bigg
  • 102,687
  • 22
  • 224
  • 252
  • 1
    I don't see how this is a solution. It breaks if there are non word characters in the URL, which is likely to have at least an '%' and you must at least have 1 dot. It's like curing a headache by chopping the head off. This is the right solution: http://stackoverflow.com/questions/5369654/why-do-routes-with-a-dot-in-a-parameter-fail-to-match – JackyJohnson Dec 24 '14 at 07:24