views:

48

answers:

2

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?

+5  A: 

Your routes are upside-down; you want more-specific routes first, and more generic routes later. Rails will take the first route it can use to match a given set of parameters.

ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/:action/:genus/:species/:id'
  map.connect ':controller/:action/:genus/:id'
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action'
  map.connect ':controller', :action => 'index'
  map.root :controller => 'main', :action => 'index'
end

That said, you could use named routes for this really easily.

ActionController::Routing::Routes.draw do |map|
  map.with_options(:path_prefix => ":controller/:action") do |con|
    con.species ":genus/:species/:id"
    con.genus   ":genus/:id"
    con.connect ":id"
    con.connect ""
  end   
end

Which gets you the following routes:

species /:controller/:action/:genus/:species/:id
  genus /:controller/:action/:genus/:id
        /:controller/:action/:id
        /:controller/:action

Then, you could just use:

<%=genus_path("main", "test", 42, 1) %>

To get:

"/main/test/42/1"
Chris Heald
Thanks, Chris, but this doesn't actually help. Reversing the order produces nicer-looking URLs, but those URLs are then not correctly interpreted when you navigate to them. The path "/main/test/42/1" gives {"controller"=>"main", "action"=>"test", "genus"=>"42", "species"=>"1"} but we wanted the "1" to match :id not :species. It seems that Rails is matching the first route even if some components are missing. Using named routes will make the routes.rb file neater, but it can't make any difference to the URL interpretation. The problem remains ...
Graham
I think I need some additional info then; if you want a route with genus/species/ID, you'd need 3 pieces of information. I made a mistake in my original reply (which I've corrected), but I'm trying to understand the nature of the problem -- Rails should only match a route when all its conditions are satisfied. In the routes above, species_path takes 5 parameters, genus_path takes 4, and the others take 3/2.
Chris Heald
I agree. Rails SHOULD only match a route when all its conditions are satisfied, but it seems that that is not the case. The path "/main/test/42/1" has four pieces of information. But when I navigate to that path, Rails tells me that "species"=>"1". This is wrong, because :species exists only in the route with five pieces of information.So my question is this. Using the un-named more-specific-first set of routes (from the first code block of your answer), I don't understand why Rails is matching the URL "/main/test/42/1" with the first line. It ought to match the second line only.
Graham
+1  A: 

It's a rather silly solution, but this should work:

ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/:action/:genus/:species/:id', :genus => /\d+/,
    :species => /\d+/, :id => /\d+/
  map.connect ':controller/:action/:genus/:id', :genus => /\d+/, :id => /\d+/
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action'
  map.connect ':controller', :action => 'index'
  map.root :controller => 'main', :action => 'index'
end

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.

molf
Brilliant, it works! I think you're right about something changing at version 2.3.8 because I'm sure I used something like this ages ago. Putting in the specific requirements will solve it until I move up to Rails 3. Many thanks.
Graham