Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing in Rails. Dots in URL [duplicate]

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.

like image 505
evfwcqcg Avatar asked Jan 03 '12 20:01

evfwcqcg


1 Answers

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.

like image 145
Ryan Bigg Avatar answered Sep 23 '22 04:09

Ryan Bigg