Let’s revisit the subject of content negotiation in Rails. Now that I’ve upgraded to Rails 1.1 I’ve had a chance to play with the new responds_to functionality. To
get some feedback, I also posted a comment to Jamis’ blog and a message to
the REST microformat list.
My conclusion is that adding content negotiation to Rails would be valuable. The new responds_to functionality is useful when you want to override the default behavior. Having content negotiation would be useful for making the default behavior more intelligent.
Templates already work in a similar manner. For example, say you write this code:
class MyController < ApplicationController def list end end
Rails will assume that you want to render a template called list.rhtml. However,
you can override this behavior as needed like this:
class MyController < ApplicationController def list render(:template => 'not_the_default_template') end end
Content negotiation would work in a similar manner. It would load the appropriate
template based on a client’s request as specified in the HTTP accept header. You
can then override that behavior when needed using responds_to.
The way I propose this works is that Content Types are mapped to templates via their file extensions. For instance:
One key thing to understand – the template extension does not dictate the template type. Thus, list.rxhtml could be an ERB template or a Builder template or any other sort of template.
Right now Rails doesn’t support such a scheme because template extensions are hard-coded to template types. This is done in ActionView::Base::create_template_source:
It would be easy to work around this code since the template parameter includes
the full source of the template. With a little regular expression magic, you could figure out what type of template has been loaded and then run the appropriate code.
However – what if we did something more clever? What if every template started
with an comment that specified its type – similar to a Unix shebang or XML processing instruction. Maybe something like this:
For ERB: <!-- template_type :erb --> For RJS: # template_type :rjs For Builder: <!-- template_type :erb -->
It would then be easy to extract out the template type when it was loaded. If the template comment did not exist (i.e., for all existing templates) then you could revert to regular expression magic.
This solution has one very nice property – it would make it really easy to add additional template types to Rails (not that its that hard now).
To recap, the idea is:
- Implement default content negotiation by mapping content types to template names via file extensions (.rhtml, .rxthml, .rjs., .ratom, etc.)
- Add code to Rails so that it determine a template’s type when the template is
- Use responds_to to override the default behavior as needed.
What do people think of this idea? If there is interest, I’ll update my content
negotiation plugin and add this functionality to Rails 1.1.