Monday, July 6, 2009

Adding Non-REST Actions to Rails Apps

So you want to refer to a non-REST action in a rails app, but you want to use map.resources? Just add this to the end of your routes.rb

map.connect ':controller/x/:action/:id'
map.connect ':controller/x/:action//:id.:format'

Rails will take care of the rest, and now you've got the best of both worlds.

link_to :action=>"search"

and you'll get


Beautiful stuff. Can't figure out why this got downvoted on StackOverflow, but c'est la vie.

Note: No, these are not the default routes that rails installs. The "x" is the difference (you can use another String, or put it elsewhere, or what have you): without the "x" the route will get "produced" correctly, but then will be "served" by the show action on the map.resourced controller.

The "correct" way to do this is to list every additional method yourself using :member, and I don't know if that even allows GET and POST for the same method.

Some links on REST:
  1. Taking Rails Too Far
  2. An idea on when to not use REST.
  3. What is the resource? That's important too.

No comments: