Tom Lianza
Tom Lianza

Reputation: 4072

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

Upvotes: 4

Views: 1226

Answers (2)

bowsersenior
bowsersenior

Reputation: 12574

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

Upvotes: 3

Zach Inglis
Zach Inglis

Reputation: 1257

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.

Upvotes: -1

Related Questions