When should I use :conditions or :requirements in rails 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
The only option
:conditionstakes is:method(i.e.:get,:post, etc.), letting you restrict which methods may be used to access the route::requirements, on the other hand, lets you specify a regular expression that the parameter must match, e.g. if the parameter is a postal code you can give it a regular expression that only matches postal codes:(You can even drop
:requirementsand use this shorter form:)Look under “Route conditions” and “Regular Expressions and parameters” in ActionController::Routing, from which I stole the above examples.