带有通配符和要求的Rails路由 [英] Rails routes with wildcard and requirements
问题描述
我需要设置匹配以下链接的路由:
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
无限的参数,但是所有参数的格式均为[number]-[some-string]。
第二种情况仅具有一个仅包含角色的参数。
First case has unlimited parameters, however all of them are in format [number]-[some-string]. Second case has only one parameter with caracters only.
我尝试过:
map.connect("/fixed/*param_list",
:controller => 'first',
:action => 'index',
:requirements => {:param_list => /(\d+-[\w-]+)/})
map.connect("/fixed/:category",
:controller => 'second',
:action => 'index')
但是,具有需求的第一条路线不再像通配符一样起作用。它只匹配ulr,如 / fixed / 922-another ,但不匹配二维或更多维的 / fixed / 922-another / 123-more 。
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.
所以我的问题是:是否可以通过需求参数化通配符路由?以及如何在我的情况下进行设置。
推荐答案
看来您的正则表达式是错误的。应该不应该是 / \d +-[\w-] +(\ / \d +-[\w-] +)* /
处理多个参数?
It seems like your regexp is wrong. Shouldn't it be /\d+-[\w-]+(\/\d+-[\w-]+)*/
in order to handle multiple parameters?
这篇关于带有通配符和要求的Rails路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!