Rails路由将完整的主机名与多个主机名匹配 [英] Rails routes match full hostname with multiple period in between
问题描述
我正在尝试匹配以下网址
I'm trying to match a URL something like
http://example.com/this.is.my.full.hostname/something/else
显然,当我在routes.rb文件中传递参数时无法识别此参数
apparently when I pass the param in the routes.rb file it doesn't recognize this parameter
我的代码显示以下内容
match '/:computer_hostname/somethingelse' => 'test#info'
任何想法是实现我上面想要的URL的正确方法是什么?甚至有可能吗?我知道URL中允许使用句点字符,但它允许多个字符吗?
any ideas what's the right way to achieve the URL I wanted above ? Or is it even possible ? I know period is allowed character in URL but does it allow more than one ?
推荐答案
我认为约束
方法/选项将帮助您。尝试执行以下操作:
I think the constraints
method/option will help you out. Try something like the following:
match ':hostname/something/else' => 'test#info',
:constraints => {:hostname => /[A-Za-z0-9\._\-]+/}
如果您正在使用相同的:hostname
段进行多个匹配,那么您可以将它们包装在一个约束方法调用中:
If you're doing multiple matches all with the same :hostname
segment, then you can wrap them in a constraints method call:
constraints(:hostname => /[A-Za-z0-9\._\-]+/) do
match ':hostname/something/else' => 'test#info'
match ':hostname/foo/bar' => 'test#foo'
end
这篇关于Rails路由将完整的主机名与多个主机名匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!