Limiting Non-Resource Controller Actions to Specific HTTP Verbs

I just discovered a useful, but poorly (if at all) documented, feature for setting up your routes in Ruby on Rails.

One of the best things about using ActiveResource for controllers is the limiting of actions to specific HTTP methods. Sometimes you have a controller that doesn’t manipulate a specific resource, but you can still use some of that RESTy goodness.


map.connect '/kiosk', :controller => 'kiosk',
                      :action => 'index',
                      :conditions => { :method => :get }
map.connect '/kiosk', :controller => 'kiosk',
                      :action => 'signin',
                      :conditions => { :method => :post }

 

Now the same URI performs two different actions based on the HTTP verb thanks to the conditions hash you can pass to the Mapper’s connect method.

No more of that if request.post? ... else ... business.

Nice and clean.

0 Responses to “Limiting Non-Resource Controller Actions to Specific HTTP Verbs”


  1. No Comments

Leave a Reply