# Tricky respond\_to and layout problem

**URL:** <https://www.ruby-forum.com/t/tricky-respond-to-and-layout-problem/122043>\
**Category:** Rails\
**Created:** [December 4, 2007, 3:48am UTC](https://www.ruby-forum.com/t/tricky-respond-to-and-layout-problem/122043 "2007-12-04T03:48:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [December 4, 2007, 3:48am UTC](https://www.ruby-forum.com/t/tricky-respond-to-and-layout-problem/122043/1 "2007-12-04T03:48:58Z")

</div>

Hi all…

I just spent the better part of an hour wrestling with a finicky  
situation, and since there was some discussion earlier regarding  
multiple layouts, I thought I’d share my experience in the hope it  
might save someone a bit of hair-pulling 🙂

In one of my controllers, I had these actions:

def welcome

end

def welcome\_xml

end

def math

end

def math\_xml

end

The actions sans “\_xml” are simple actions that render a view. The  
only difference between them is the layout used: welcome uses a one  
column layout while math uses a two column layout. The two actions  
that do have “\_xml” are for generating xml packets of some statistics  
that are related to the non-xml actions. They are sort of like a web  
service in that they will be consumed by external sources (flash  
movies, etc). As it was, I didn’t want the xml actions rendered in a  
layout, which is where my choose\_layout method came from:

def choose\_layout  
if action\_name == ‘welcome’  
‘one\_column’  
elsif ![‘welcome\_xml’, ‘math\_xml’].include?(action\_name)  
‘two\_column’  
else  
nil  
end  
end

Using that, I got the results I desired. Until the “let’s do things  
the right way” bug bit me. I looked at the xml actions and thought,  
“I should be using respond\_to”, so I set about making the  
modifications. I changed the non xml actions to be

def welcome/math

# do something

respond\_to do |format|  
format.html  
format.xml  
end  
end

and made sure that my view files were welcome.rhtml/rxml and  
math.rhtml/rxml. The stage was set for rails goodness to “Just  
Work”…but it didn’t. When I hit /controller/math.xml, I got an  
error about malformed xml. As it turned out, the xml was being  
rendered in a layout. Since the action names were no longer  
‘welcome\_xml’ and ‘math\_xml’, the choose layout method wasn’t working  
as it once did. So I tried this:

format.xml {render :layout =\> false}

thinking that the xml view would be rendered without a layout. Well,  
it was, almost. A layout was not used, but for some reason that I  
could not determine, the rhtml view was rendered instead of the rxml  
view, even though my url was /controller/math.xml.

Much googling didn’t really turn up a silver bullet, but enough  
little pricks in the brain were generated to get some ideas  
formulating. I did some experimenting with action\_name and concluded  
that even though I used math.xml, the action name was still math.  
The only way to tell the difference between the requests was to know  
what the format is. On a whim, I inspected params[:format] when I  
hit /controller/math.xml. And there it was, as big as life, “xml”.  
Woohoo! I ended up changing my choose\_layout method to consider params  
[:format], like so:

def choose\_layout  
if params[:format] == ‘xml’  
nil  
elsif action\_name == ‘welcome’  
‘one\_column’  
else  
‘two\_column’  
end  
end

and taking the {render :layout =\> false} off of the format.xml line.  
So now it all works the way I expect, and want, it to.

Hopefully this will help someone (and maybe make up for my silly  
mistakes earlier today 😉 ).

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [December 6, 2007, 7:26am UTC](https://www.ruby-forum.com/t/tricky-respond-to-and-layout-problem/122043/2 "2007-12-06T07:26:24Z")

</div>

Hi,

On Dec 3, 2007 9:48 PM, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def welcome
> 
> movies, etc). As it was, I didn’t want the xml actions rendered in a  
> end  
> format.html  
> as it once did. So I tried this:
> 
> format.xml {render :layout =\> false}

format.xml { render :xml =\> @foo.to\_xml }

> The only way to tell the difference between the requests was to know  
> else  
> Peace,  
> Phillip
> 
> > 
> 
> HTH,  
> Michael G.

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [December 6, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/tricky-respond-to-and-layout-problem/122043/3 "2007-12-06T13:47:43Z")

</div>

Hi Michael,

On Dec 6, 2007, at 12:25 AM, Michael G. wrote:

> format.xml {render :layout =\> false}
> 
> format.xml { render :xml =\> @ foo.to\_xml }

If I had a single object that could be rendered to xml, then, yes,  
this would have been the simplest and most correct approach. But as  
it turns out, there is some logic in my rxml view that does some  
calculations before it renders the xml. I’ll do some checking to see  
if there is an easier way for me to accomplish what I need to.

Thanks for the comment.

Peace,  
Phillip
