简体   繁体   中英

Rails Routing with a parameter that contains a period

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

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([^/;,?]+)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM