Earlier in the week I posted about making dynamic RESTful routes in Rails. At the end of the article, I mentioned that the method I was using might not work in a production environment, and after searching Agile Web Development with Rails for the word “restart,” I confirmed my suspicions. Generating routes on the fly from data runs afoul of caching, requiring the server to be restarted to pick up changes to the routing. That rules it out as a reliable way to generate pretty URLs.
To recap, I’ve already used map.resources to generate the usual http://example.com/sections/1 style of REST URL. I want to be able to route a URL like http://example.com/about to the show method of my Sections controller, as well, to improve human readability (and SEO) of the URL.
Originally published at nyerm. You can comment here or there.
I’m slowly becoming a convert to the RESTful routing model in recent versions of Rails, largely because it builds a pile of useful named routes with a tiny amount of code. If I put map.resources :sections in my routes.db file, I instantly get sections_path, edit_section_path, and all their friends. When coding a controller, it’s a lot nicer to write redirect_to section_path than redirect_to :controller => :section, :action => show.
The only difficulty I have with default routes created by map.resources is that, in some cases, they’re ugly and show too much of the application’s internal workings. For example, the application I’m working on now has a Section model, which describes a major section heading in the site (things like About, FAQ, and Help). Instead of having weird routes like /sections/1, I want human-readable URLs like /about. They look nicer, and they’re better for SEO.
Originally published at nyerm. You can comment here or there.
