# Customizing layouts for functions

**URL:** <https://www.ruby-forum.com/t/customizing-layouts-for-functions/71255>\
**Category:** Rails\
**Created:** [February 21, 2006, 12:53pm UTC](https://www.ruby-forum.com/t/customizing-layouts-for-functions/71255 "2006-02-21T12:53:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cranberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@cranberry](https://www.ruby-forum.com/u/cranberry)\
**Post date:** [February 21, 2006, 12:53pm UTC](https://www.ruby-forum.com/t/customizing-layouts-for-functions/71255/1 "2006-02-21T12:53:56Z")

</div>

I want to be able to have different layouts in my rails program for  
different functions.

For example, when a person first comes to the site the layout will have  
a link that says ‘home’, ‘login’, or register on the menu bar.

Once the user logs in however, the layout will change. I want it to show  
the links ‘account manager’, ‘logout’, etc.

I tried putting "layout ‘layout-name’ " right after I define a function,  
but rails ddin’t like this.

Any suggestions?

Thank you!

---

<div class="post-metadata">

**Author:** ![cranberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@cranberry](https://www.ruby-forum.com/u/cranberry)\
**Post date:** [February 21, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/customizing-layouts-for-functions/71255/2 "2006-02-21T12:58:28Z")

</div>

You can either use

render :layout =\> ‘custom\_layout’

in each of your actions, or in your controller itself:

class MyController \< ApplicationController  
layout :choose\_layout  
def choose\_layout  
# return the string based on the action in params or whatever  
end  
…  
end

- james

On 2/21/06, cranberry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but rails ddin’t like this.  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![cranberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@cranberry](https://www.ruby-forum.com/u/cranberry)\
**Post date:** [February 21, 2006, 1:04pm UTC](https://www.ruby-forum.com/t/customizing-layouts-for-functions/71255/3 "2006-02-21T13:04:33Z")

</div>

James A. wrote:

> You can either use
> 
> render :layout =\> ‘custom\_layout’
> 
> in each of your actions, or in your controller itself:
> 
> class MyController \< ApplicationController  
> layout :choose\_layout  
> def choose\_layout  
> # return the string based on the action in params or whatever  
> end  
> …

Thank you! that is so easy.

---

<div class="post-metadata">

**Author:** ![cranberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@cranberry](https://www.ruby-forum.com/u/cranberry)\
**Post date:** [February 21, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/customizing-layouts-for-functions/71255/4 "2006-02-21T15:02:08Z")

</div>

cranberry wrote:

> James A. wrote:
> 
> > You can either use
> > 
> > render :layout =\> ‘custom\_layout’

But I have a few functions in the controller that each need their own  
separate layout …

isn’t there a way to specify in the function itself what the layout  
should be? I want the layout to be in the function itself, otherwise i  
have to go and find all the instances of in my program and add ‘:layout  
=\>’ to them…

Suggestions?

---

<div class="post-metadata">

**Author:** ![cranberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@cranberry](https://www.ruby-forum.com/u/cranberry)\
**Post date:** [February 21, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/customizing-layouts-for-functions/71255/5 "2006-02-21T17:56:04Z")

</div>

cranberry wrote:

> should be? I want the layout to be in the function itself, otherwise i  
> have to go and find all the instances of in my program and add ‘:layout  
> =\>’ to them…

Look at James’ post again:

"You can either use

render :layout =\> ‘custom\_layout’

in _each of your actions_, or in your controller itself…"

In other words, “action” = “function”. (And actually, you should call  
them “methods” in  
Ruby… although in the rails controller case, we use each method as an  
“action”.)

b
