# Controlling layout based on format

**URL:** <https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757>\
**Category:** rubyonrails-core\
**Created:** [March 6, 2007, 9:41am UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757 "2007-03-06T09:41:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexey\_Verkhovsky](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alexey\_Verkhovsky](https://discuss.rubyonrails.org/u/Alexey_Verkhovsky)\
**Post date:** [March 6, 2007, 9:41am UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/1 "2007-03-06T09:41:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mislav](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mislav](https://discuss.rubyonrails.org/u/Mislav)\
**Post date:** [March 6, 2007, 10:05am UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/2 "2007-03-06T10:05:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![akaspick](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@akaspick](https://discuss.rubyonrails.org/u/akaspick)\
**Post date:** [March 6, 2007, 5:32pm UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/3 "2007-03-06T17:32:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Okada](https://avatars.discourse-cdn.com/v4/letter/o/3be4f8/32.png) [@Okada](https://discuss.rubyonrails.org/u/Okada)\
**Post date:** [March 7, 2007, 5:47pm UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/4 "2007-03-07T17:47:13Z")

</div>

I use format.js for rjs.

---

<div class="post-metadata">

**Author:** ![Alexey\_Verkhovsky](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alexey\_Verkhovsky](https://discuss.rubyonrails.org/u/Alexey_Verkhovsky)\
**Post date:** [March 7, 2007, 6:52pm UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/5 "2007-03-07T18:52:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dan\_Manges1](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@Dan\_Manges1](https://discuss.rubyonrails.org/u/Dan_Manges1)\
**Post date:** [March 7, 2007, 6:55pm UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/6 "2007-03-07T18:55:49Z")

</div>

Are you using Rails 1.2.2? Did you try exempt\_from\_layout :rxml in your controller?

Dan Manges

---

<div class="post-metadata">

**Author:** ![Alexey\_Verkhovsky](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alexey\_Verkhovsky](https://discuss.rubyonrails.org/u/Alexey_Verkhovsky)\
**Post date:** [March 8, 2007, 1:22am UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/7 "2007-03-08T01:22:00Z")

</div>

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

Alex

---

<div class="post-metadata">

**Author:** ![akaspick](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@akaspick](https://discuss.rubyonrails.org/u/akaspick)\
**Post date:** [March 8, 2007, 1:50am UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/8 "2007-03-08T01:50:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mislav](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mislav](https://discuss.rubyonrails.org/u/Mislav)\
**Post date:** [March 8, 2007, 9:39am UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/9 "2007-03-08T09:39:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mislav](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mislav](https://discuss.rubyonrails.org/u/Mislav)\
**Post date:** [March 8, 2007, 9:40am UTC](https://discuss.rubyonrails.org/t/controlling-layout-based-on-format/9757/10 "2007-03-08T09:40:15Z")

</div>

Did I say “unit”? I meant functional.
