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?
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.
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