In Rails 3: How do I render with layout in respond_with using a custom format? – Education Career Blog

My problem is similar to the one in this question, but the opposite.
How do I specify “:layout => false” in Rails’ respond_with?

The controller is sort of a file converter. It responds to many formats. One format is supposed to render an html layout showing metadata. This format need the layout to be rendered, unlike most other formats. Problem is I can’t convince Rails to render it.

class CustomFilesController < ApplicationController
  respond_to :all, :only => :show
  def show
    @custom_file = CustomFile.find(params:id)
    respond_with(@custom_file) do |format|
      format.html {}
      format.xml {}
      format.json {}
      format.fil {
        puts '>>>>> IN FIL <<<<<'
        render :layout => true
      }
      format.any {
        file = @custom_file.as(:type => params:format, :size => params:size) ## the REAL path to the file
        (file.nil? || @custom_file.nil?) ? head(:not_found) : send_file(file, :disposition => 'inline', :url_based_filename => true)
      }
    end
  end
end

I thought “:layout => true” would work but apparently not.

As you can see I have stripped away everything else in the controller before pasting it in here. The format “fil” is added to the mime types as a text/html format in config/initializers/mime_types.rb. I have my view file in place and it renders, but without any layout around it.

I would appreciate any advice. I have gotten nowhere for a while now and anything I find online suggests my code should work.

Thanks in advance.

,

Found that the layout renders if I specify the exact path and filename of the layout file. In my case:

render :layout => '/layouts/application.html.haml'

Leave a Comment