带有包含句点的参数的 Rails 路由
在旧版本的 Rails 中,您可以有一个包含句点的参数(通常保留以将其与格式分开),如下所示:(
map.connect 'c/:domain.:format', :controller=>'home', :action=>'click_credit', :requirements => { :domain => %r([^/;,?]+) }
很好的教程 这里)
但是,在更现代的 Rails 版本(2.3.x)中,我看到这个失败 - :domain 正在捕获所有内容,并且:当请求 /c/amazon.com.html 时格式为空
关于如何修改它有什么想法吗?
谢谢, 汤姆
In older versions of Rails, you could have a parameter that includes a period (something typically reserved to separate it from the format) like this:
map.connect 'c/:domain.:format', :controller=>'home', :action=>'click_credit', :requirements => { :domain => %r([^/;,?]+) }
(nice tutorial here)
However, in more modern versions of Rails (2.3.x) I'm seeing this fails - :domain is catching everything, and :format is blank when the request comes in for /c/amazon.com.html
Any ideas on how to modify it?
Thanks,
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
升级到 Rails 3 应该可以解决您的问题。我在 Rails 3 中尝试了您的路线,效果很好(稍作更改即可使用新的路由语法):
如果无法升级到 Rails 3,那么这可能会有所帮助。我认为根据这个答案
:requirements
哈希可能是罪魁祸首。尝试这条路线,看看它是否有效:Upgrading to rails 3 should solve your problem. I tried your route in rails 3 and it worked fine (with a minor change to use the new routing syntax):
If upgrading to rails 3 is not an option, then this might help. Per this answer, I think the
:requirements
hash may be the culprit. Try this route and see if it works:您需要将域名更改为别名,并将任何[句号]替换为[斜杠]。
原因是因为浏览器期望 .定义文件类型,您无法解决此问题。
使用这个 gem 让您变得轻松。
You need to change the domain into a slug and replace any [period] with a [slash].
The reason is because browsers expect . to define file type and you can not work around this.
Use this gem to make it easy for you.