Skip to content

Instantly share code, notes, and snippets.

@cyberkni
Created July 7, 2009 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberkni/142246 to your computer and use it in GitHub Desktop.
Save cyberkni/142246 to your computer and use it in GitHub Desktop.
## Configuration
This is on Rails 2.3.2.1 running on ruby 1.8.4
## in config/routes.rb
map.somecontroller_show 'somecontroller/:id.:format',
:requirements => {:id => /\w[^\/?;]*/ },
:controller => 'somecontroller', :action => 'show',
:conditions => { :method => :get }
## the problem
:id for this controller is a domain name. The path to this route will look like this:
GET /somecontroller/somehost.mydomain.com.xml - for when the user wants a specific format
or
GET /somecontroller/somehost.mydomain.com - for when the user requests the default format
For the first path params should be {:action => 'show', :controller => 'somecontroller', :id => 'somehost.mydomain.com', :format => 'xml'} but end up being {:action => 'show', :controller => 'somecontroller', :id => 'somehost.mydomain.com.xml'}
For the second path params should be {:action => 'show', :controller => 'somecontroller', :id => 'somehost.mydomain.com'} and work properly.
The problem is with this route I can not extract the format because it is getting matched by the :id regular expression. I could change the :id regular expression to be not greedy which will fix the first path but break the second one because it will extract 'com' as the format.
How do you use :format when your URI contains domain names?
I've hit this issue while upgrading from Rails 2.1.2 to 2.3.2.1. Historically my site has just had two routes, one for the route with format and one without.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment