Upgrading to Rails 2.0: template considerations
Rails 2.0 has better support for multiviews. I was aware of this having briefly read about it in the Rails blog, but didn’t pay a ton of attention until something wasn’t working the way it used to. I had a controller action that I wanted handeled by two different templates depending on the request type: one for html and one for js. Both templates had the same name - show.rhtml and show.rjs, but show.rjs was not being rendered for an XHR request. According to the docs you didn’t have to change to the new naming conventions but it seems in this case you have to.
Prior to 2.0, you could have an action such as:
def index respond_to do |format| format.html format.js end end
and two templates, index.rhtml and index.rjs and the appropriate template would be rendered depending on the request type. You can’t do that now. What you have to do is rename index.rhtml to index.html.erb. That will allow you to have to templates with the same name. It’s probably a better idea to just follow the new convention to avoid any problems, which in this case means also renaming index.rjs to index.js.rjs.
I also has a template with an rxml extension which was also not being rendered correctly. Renaming to show.xml.builder solved the problem.
Here’s a quote from the 2.0 announcement that covers this:
We’ve separated the format of the template from its rendering engine. So show.rhtml now becomes show.html.erb, which is the template that’ll be rendered by default for a show action that has declared format.html in its respond_to. And you can now have something like show.csv.erb, which targets text/csv, but also uses the default ERB renderer.
So the new format for templates is action.format.renderer. A few examples:
* show.erb: same show template for all formats
* index.atom.builder: uses the Builder format, previously known as rxml, to render an index action for the application/atom+xml mime type
* edit.iphone.haml: uses the custom HAML template engine (not included by default) to render an edit action for the custom Mime::IPHONE format
Post a Comment