Controlling layout based on format

When selecting either RHTML or RXML template in a respond_to block, I intuitively expect RJS and RXML templates to be rendered as they are. However, it doesn’t work that way. Layout associated with the action is always included.

After some head-scratching I got around it by creating a bogus action and rendering its view, like this:

def index respond_to |format| do format.rhtml format.xml { render :template => ‘index_xml.rxml’, :layout => false }

end end

private def index_xml raise “This is not a valid action, just a workaround for rendering index.xml without layout, don’t use it” end

instead of

def index respond_to |format| do format.rhtml format.xml end end

This sucks.

Generally, “no layout” seems like a reasonable default for RJS and RXML templates to me. I have yet to see anyone using those things with a layout, so the current behavior usually results in extra code to switch it off. Thoughts?

Sorry if this was discussed before.

Alex

RXML isn’t just for feeds and data dumps - people use it for XHTML, also. But I agree with you that RJS responses shouldn’t have implicit layout.

Maybe we should teach respond_to about this: include a template only for the main response (usually HTML), but implicitly turn of layout for others. People could then explicitly choose a layout for them.

There's an @@exempt_from_layout variable in ActionController, which is supposed to exempt rjs by default, but I believe it doesn't work.

I haven't been able to get it working in my applications. I think there's an open ticket (or possibly a recently closed one) for it.

Andrew

I use format.js for rjs.

OK, RJS is not a problem, and there may be a case where RXML with layout makes sense (does that happen often enough to make it a good default? I doubt).

I still say that formatting something with RXML without layout should not require creating a bogus action. It just doesn’t feel right. How about

format.xml :layout => nil

Alex

Are you using Rails 1.2.2? Did you try exempt_from_layout :rxml in your controller?

Dan Manges

Are you using Rails 1.2.2?

Yes

Did you try exempt_from_layout :rxml in your controller?

No. And I hear what you say. :slight_smile:

Alex

exempt_from_layout works for you with rjs? rjs is supposed to be exempt by default, but I've never got it working.

In my app I have this in my application controller to remove layout for rjs... layout lambda { |controller| controller.request.xhr? ? nil : 'main' }

If I change that to just...

layout 'main'

My rjs actions still return a layout. Even if I add "exempt_from_layout :rjs"

Maybe I misunderstand this method. :confused:

Check the unit tests. If there are none for this, maybe it’s time to write some new ones

Did I say “unit”? I meant functional.