REST design with extra actions

Yesterday, I had to add some extra actions in my controllers, so I could view some charts. The problem was that Rails didn’t recognise the actions, because Rails sees you action name as an id.

But Rails wouldn’t be Rails if it didn’t had a clever solution. Lets say you have a a resource called “Resource” and a similar controller. You want to add a new action and view to the resource controller named “charts” .

Just open your routes.rb file and find the line where you map the resource and add the following:

map.resources :resource, :collection => [:charts], :member => [:print_pdf]

For the sake of being complete, if have added a member. The difference between a collection and a member is that collections work on all items of that resource (no id needed, like index action), so:


could be used and give you a page with covering the charts of all resources while a member works on a single item of the resource, defined by it’s id


would print the resource with id 1234 as pdf of course like with all other REST actions (or CRUD actions, to be correct) you can use additional named params like:

chart_resources_path(:type => :month) 
print_pdf_resource_path(1234, :font => "Arial")