Rails routes with wildcard and requirements

99封情书 提交于 2019-12-11 10:23:51

问题


I need to set up routes wich will match links like this:

1: fixed/9731-monday-tuesday-wednesday-thursday-friday/922-another
2: fixed/potatoe

First case has unlimited parameters, however all of them are in format [number]-[some-string]. Second case has only one parameter with caracters only.

I'v tried this:

   map.connect("/fixed/*param_list",
               :controller => 'first',
               :action => 'index',
               :requirements => {:param_list => /(\d+-[\w-]+)/})

   map.connect("/fixed/:category",
               :controller => 'second',
               :action => 'index')

However first route with requirements doesn't work as with wildcard any more. It only match ulr like /fixed/922-another but not two and more dimensional /fixed/922-another/123-and-more.

So my question is: Is possible to parametrized wildcard route via requirments? And how to set up it in my case.


回答1:


It seems like your regexp is wrong. Shouldn't it be /\d+-[\w-]+(\/\d+-[\w-]+)*/ in order to handle multiple parameters?



来源:https://stackoverflow.com/questions/5326679/rails-routes-with-wildcard-and-requirements

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