Where's the Glue?

Posted July 26, 2006

Perhaps I'm out of the loop on this one, but I've found an odd gap between edge Rails and the simply_restful plugin. Here's what's up:

The Controller

ShoesController < ApplicationController
  def show
    @shoe = Shoe.find @params[:id]
The Views

My Expectation

When I GET /shoes/1.xml I get the XML format via the show.rxml file, and when I GET /shoes/1.html I get the HTML format via the show.rhtml file -- afterall, the format is specified in the request, and passed through as @params[:format].

Unfortunately, I don't have deep knowledge of the internal Rails code (yet!), so I kludged together a quick workaround that automatically determines format-based views.

The Workaround

def renderformatted
  @params[:format] ||= "html"  # Default format
  # build the preferred template path
  template = "#{RAILSROOT}/app/views/#{@params[:controller]}/#{@params[:action]}.r#{@params[:format]}"
  if File.exist? template
    render :file => template
    render :text => "'#{@params[:format]}' format not available for '#{@request.path}'", :status => 404
In practice, #render_formatted would be called at the end of any given action to provide the appropriate response. Know a better way to do this? Any feedback would be appreciated, especially if I'm missing a vital link in the clue train ...


comments powered by Disqus