带有包含句点的参数的 Rails 路由

发布于 2024-10-08 10:08:41 字数 461 浏览 0 评论 0原文

在旧版本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携余温的黄昏 2024-10-15 10:08:41

升级到 Rails 3 应该可以解决您的问题。我在 Rails 3 中尝试了您的路线,效果很好(稍作更改即可使用新的路由语法):

match 'c/:domain.:format', 
      :controller=>'home', 
      :action=>'click_credit', 
      :domain => %r([^/;,?]+)
# 'c/amazon.com.html' => domain: amazon.com , format: html

如果无法升级到 Rails 3,那么这可能会有所帮助。我认为根据这个答案 :requirements 哈希可能是罪魁祸首。尝试这条路线,看看它是否有效:

map.connect 'c/:domain.:format', 
            :controller=>'home', 
            :action=>'click_credit', 
            :domain => %r([^/;,?]+)

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):

match 'c/:domain.:format', 
      :controller=>'home', 
      :action=>'click_credit', 
      :domain => %r([^/;,?]+)
# 'c/amazon.com.html' => domain: amazon.com , format: html

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:

map.connect 'c/:domain.:format', 
            :controller=>'home', 
            :action=>'click_credit', 
            :domain => %r([^/;,?]+)
梦里°也失望 2024-10-15 10:08:41

您需要将域名更改为别名,并将任何[句号]替换为[斜杠]。

原因是因为浏览器期望 .定义文件类型,您无法解决此问题。

使用这个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文