Migrating my project’s codebase to Rails 3 beta from Rails 2.3.x was fairly painless except for the changes to the router.
Most files only needed a few changes, but my highly customized routes.rb file was a four hour ordeal that I hope this post can help save someone else time as Rails 3 documentation for declaring methods for named routes is sparse at best right now.
The premise of what my app needs to have happen is the RESTful routes that are automatically created by scaffolding need to be modified to live at my domain’s root without relying on any web server tricks. For the rest of this post let’s focus on just one of these overrides: the “new” verb for the “events” controller.
Default route created via scaffolding is:
http://domain.com/events/new
However, I want the new_event helper to point to:
http://domain.com/new
In Rails 2.3.x this is accomplished via declaring a named route with a conditions statement restricting to only ‘GET’ requests:
map.new_event 'new.:format',
:controller=>"events",
:action=>"new",
:conditions => {:method => :get}
In Rails 3, the new way to do named routes looks like:
match 'new(.:format)' => 'events#new',
:as => 'new_event'
Seems simple, but when the conditions statement restricting to only the ‘GET’ method is added it’s ignored.
The following does NOT work:
match 'new(.:format)' => 'events#new',
:as => 'new_event',
:conditions => {:method => :get}
In Rails 3 the ‘method’ condition is ignored by design because the proper way to limit the named route override to a specific http method is:
get 'new(.:format)' => 'events#new',
:as => 'new_event'
The beginning keyword can be any one of the http methods: get, post, put, or delete
Pretty sexy to say the least!, just wish it was better documented. Hope this helps others out.?
—————–
now playing: Hans Zimmer & James Newton Howard – Molossus