18 August 2006

Using Non-CRUD Actions With Simply Restful Controllers

I've been making use of the simply restful integration in my Rails apps. However, one of the things that took a bit of digging to figure out, was how to map additional (non-CRUD) actions. If you try to simply use a URL of /controller/action that won't work, because it'll treat the action as an ID.

I came across this blog entry on Ryan's Scraps, which explained how to add more actions to the mix. That got me most of the way there. However, what happens if you want to support both GET and POST to an action? A good example of this is a login form. You want GET to show the form page, and POST to do a submit to the login action. As it turns out, you can do this easily with the following mapping:

map.resources :users, :new => { :login => :any }

For more good info, and more of a primer, on simply_restful, check out David Goodlad's post.

technorati tags:,