Rails Routing: what is the difference between: conditions and requirements in routing?

When should I use: conditions or: requirements in rail routing?

Here are two examples:

: conditions

map.connect "/foo/:view/:permalink", :controller => "foo",
    :action => "show", :view => /plain|fancy/,
    :permalink => /[-a-z0-9]+/,
    :conditions => { :method => :get }
end

: requirements

 map.connect 'posts/index/:page',
            :controller => 'posts',
            :action => 'index',
            :requirements => {:page => /\d+/ },
            :page => nil
 end
+3
source share
1 answer

The only option :conditionsaccepts :method(i.e. :get, :postetc.), allowing you to limit which methods can be used to access the route:

map.connect 'post/:id', :controller => 'posts', :action => 'show',
            :conditions => { :method => :get }

:requirements, on the other hand, allows you to specify a regular expression that the parameter must match, for example. if this parameter is a zip code, you can give it a regular expression that matches only zip codes:

map.geocode 'geocode/:postalcode', :controller => 'geocode',
            :action => 'show', :requirements => { :postalcode => /\d{5}(-\d{4})?/ }

( :requirements :)

map.geocode 'geocode/:postalcode', :controller => 'geocode',
            :action => 'show', :postalcode => /\d{5}(-\d{4})?/

" " " " ActionController:: Routing, .

+10

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


All Articles