开发者

Why do my Rails 2 routes have query strings?

开发者 https://www.devze.com 2023-01-14 05:22 出处:网络
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

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 pa开发者_开发问答ge 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?


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"


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消