Renaming Rails resources
May 5, 2011
On a project, I have a “Revision” resource and URL’s like
Now suddenly the client wants to rename “revision” to “edition”, so all URLs should be changed to the format
Unless I’m missing something in the Rails docs, there is no option to change the base part of the path without renaming the resource itself. Actions can be renamed, eg. “nuevo” instead of “new”, but I don’t think the path segment for the resource name can be changed (please correct me if I’m wrong).
I guess the proper way to go would be to rename everything: The AR model, all helper calls, controllers, tests etc. Even with lots of automation, this would be a really annoying task and I’m not inclined to spend time on this right now. Who knows if the client will change his mind again?
So instead, I ended up changes the resource from
resources :editions, :controller => "revisions", :as => "revisions"
I’m changing the name of the resource, but the :controller option makes sure the right controller is still called, and the :as option controls the generation of path helpers (so I can keep using eg. edit_revision_path).
Remember to make the same change for nested resources.
EDIT: As Rasmus and Jakob point out, the :path option sets the actual path, not a prefix. I have made some minor updates to the Rails docs to correct this (most instances were already fixed).