zerosum dirt(nap)

evolution through a series of accidents

zerosum dirt(nap)

Going Solo: Resources Without The 'S'

February 13, 2007 by nap · Comments

In case you missed it in the release announcement for Rails 1.2.2 last week, singular resources are now available in ActionController. You can use them to model singleton resources in your application.

So we’re using technoweenie’s restful_authentication plugin to handle all things authentication-related in our current project, and decided that a singular resource was a good fit for a Session (login, logout) and Account Controller.

Here’s a dumbass-simple example:

map.resource :account, :member => { :activate => :any }

Note the missing ‘s’. No collections for us here, no sir. But what do we get?

GET /account => AccountController#show
GET /account/new => AccountController#new
POST /account => AccountController#create
GET /account;edit => AccountController#edit
PUT /account => AccountController#update
DELETE /account => AccountController#delete
(ANY) /account;activate => AccountController#activate

Hey, that’s just what I needed. And of course we get the standard named routes for the resource like account_url, account_path, etc. Yay for Resources. Yay for REST.

blog comments powered by Disqus