# Two layouts for one controller

**URL:** <https://www.ruby-forum.com/t/two-layouts-for-one-controller/121817>\
**Category:** Rails\
**Created:** [December 1, 2007, 8:48am UTC](https://www.ruby-forum.com/t/two-layouts-for-one-controller/121817 "2007-12-01T08:48:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 1, 2007, 8:48am UTC](https://www.ruby-forum.com/t/two-layouts-for-one-controller/121817/1 "2007-12-01T08:48:12Z")

</div>

Hi all,

I am implementing the functionality of content management through  
FCKeditor.  
I have a controller called aboutus and the corresponding table in the  
database for accessing the contents. Layouts of back end and front end  
are different.  
Now in the front end when i am trying to access aboutus page it is  
showing that page but with the backend layout.  
I want to show the about us page with the layout of front end.For this I  
need some method so that I can access two layouts for a single  
controller.Please tell me if there is any method to implement it.

Any help would be greatly appreciated.

Thanks,  
Rohit.

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 1, 2007, 11:31am UTC](https://www.ruby-forum.com/t/two-layouts-for-one-controller/121817/2 "2007-12-01T11:31:27Z")

</div>

On Nov 30, 2007, at 11:48 PM, Rohit M. wrote:

> controller.Please tell me if there is any method to implement it.  
> In the controller add this line

layout :choose\_layout

then add this method

def choose\_layout  
if action\_name == ‘login’  
return ‘2colwn’  
else  
return ‘1col’  
end  
end

Of course, change it for your action and layout names.

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 1, 2007, 12:36pm UTC](https://www.ruby-forum.com/t/two-layouts-for-one-controller/121817/3 "2007-12-01T12:36:24Z")

</div>

You can also do  
layout :some\_layout, :only =\> [:new, :edit]  
or  
layout :some\_other\_layout, :except [:list, :show]

And of course at the end of your action you can just say  
render :layout =\> ‘something\_else’

Fred

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 3, 2007, 5:43am UTC](https://www.ruby-forum.com/t/two-layouts-for-one-controller/121817/4 "2007-12-03T05:43:49Z")

</div>

Hi,

There is still a problem with the above code.  
If I write in my code  
layout =\> ‘store’, :only =\> [:sign\_up]  
layout =\> ‘login’, :except =\> [:sign\_up]

then both of them together doesn’t work. Only one of them works at a  
time, but I want store layout for sign\_up page and login layout for all  
other pages.

Please provide some solution.

Thanks,  
Rohit.

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 3, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/two-layouts-for-one-controller/121817/5 "2007-12-03T14:31:55Z")

</div>

Hi Rohit,

Here’s how I did it:

in application.rb

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

then in the controller

before\_filter :choose\_layout

I put the choose\_layout filter last as there’s no point in choosing a  
layout if one of the other filters might fail. The local variable  
action\_name is actually a Rails provided global-ish variable, so you  
don’t need to set it yourself. If you have to make a decision  
between actions that are named the same in multiple controllers, you  
will also need to evaluate which controller is active. I have not  
required that yet, so I don’t know exactly how to do it. I would  
like to think it’s as easy as controller\_name (to go along with  
action\_name), but I’m only guessing. In my testing, it was important  
to return nil if I didn’t want to use a layout, but I don’t have an  
API reference or any other documentation to prove that.

You can just as easily do an "if action\_name == " instead  
of using include?. At one time I had more than one action that was  
using the ‘one\_column’ layout and when I changed it, I didn’t change  
the conditional to a singleton test. I probably should as I expect a  
simple equality test takes fewer processor cycles than include?.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 1, 2007, 12:59pm UTC](https://www.ruby-forum.com/t/two-layouts-for-one-controller/121817/6 "2007-12-01T12:59:15Z")

</div>

Frederick C. wrote:

> You can also do  
> layout :some\_layout, :only =\> [:new, :edit]  
> or  
> layout :some\_other\_layout, :except [:list, :show]
> 
> And of course at the end of your action you can just say  
> render :layout =\> ‘something\_else’
> 
> Fred

Hi,

Thanks a lot Fred. Its working now.

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 4, 2007, 1:47am UTC](https://www.ruby-forum.com/t/two-layouts-for-one-controller/121817/7 "2007-12-04T01:47:55Z")

</div>

I made a typo in my earlier post. Instead of

> before\_filter :choose\_layout

I should have written

layout :choose\_layout

Sorry if this caused any confusion.

Peace,  
Phillip
