Why do my Rails 2 routes have query strings?

I sometimes need to pass additional parameters to the page at the URL. In the past, I have done this with several generic placeholders in the routes file, which I call "genus" and "view". This worked, but now he began to create URLs with query strings.

Rails version is 2.3.8.

Route File:

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

Index Page:

<p>
<%= url_for :controller => 'main', :action => 'test', :genus => 42, :id => 1 %>
</p>

Testing Page

<p>
<%= params.inspect -%>
</p>

Does the index page display / main / test? genus = 42 & id = 1 , where I would expect / main / test / 42/1 .

However, if I go to / main / test / 42/1 , then I see the correct options:

{ "controller" = > "main", "action" = > "test", "genus" = > "42", "id" = > "1" }

, ?

+3
2

, :

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

, Rails 2. , Rails 3. - , , 2.3.5-2.3. 8.

+1

; . Rails , .

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

, .

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

:

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

:

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

:

"/main/test/42/1"
+5

Source: https://habr.com/ru/post/1762695/


All Articles