I sometimes need to pass additional parameters to a page via the URL. I did this in the past with a couple of generic placeholders in the routes file, which I call “genus” and “species”. This used to work, but now it has started producing URLs with query strings.
The Rails version is 2.3.8.
The routes file is:
ActionController::Routing::Routes.draw do |map|
map.root :controller => 'main', :action => 'index'
map.connect ':controller', :action => 'index'
map.connect ':controller/:action'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:genus/:id'
map.connect ':controller/:action/:genus/:species/:id'
end
The index page is:
<p>
<%= url_for :controller => 'main', :action => 'test', :genus => 42, :id => 1 %>
</p>
The test page is
<p>
<%= params.inspect -%>
</p>
The index page shows /main/test?genus=42&id=1 where I would have expected /main/test/42/1.
However, if I go to /main/test/42/1 then I see the correct parameters:
{“controller”=>”main”, “action”=>”test”, “genus”=>”42”, “id”=>”1”}
Any ideas what I’m doing wrong?
It’s a rather silly solution, but this should work:
The problem is that all components of the routes are optional in Rails 2. Fortunately this is better in Rails 3. My guess is something that has to do with the priority of the routes has changed around version 2.3.5-2.3.8.