Routing in Rails. Dots in URL [duplicate]

拜拜、爱过 提交于 2019-12-01 00:25:52

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8718093/routing-in-rails-dots-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!