# Layout declaration

**URL:** <https://www.ruby-forum.com/t/layout-declaration/179265>\
**Category:** Rails\
**Created:** [November 20, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/layout-declaration/179265 "2009-11-20T17:58:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cristie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@cristie](https://www.ruby-forum.com/u/cristie)\
**Post date:** [November 20, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/layout-declaration/179265/1 "2009-11-20T17:58:11Z")

</div>

Hi all.

We have been trying to do a custom layout for special user’s via  
something like this:

class ProductsController \< ApplicationController

def show  
products\_layout  
@product = Product.find(params[:id])  
end

private def products\_layout  
layout ‘special’ if @current\_user.special?  
layout ‘not\_special’ unless @current\_user.special?  
end

end

We got a ‘no such method error’ on layout.

via the url below:

> **[Layouts and Rendering in Rails — Ruby on Rails Guides](https://guides.rubyonrails.org/layouts_and_rendering.html)**
>
> Layouts and Rendering in RailsThis guide covers the basic layout features of Action Controller and Action View.After reading this guide, you will know: How to use the various rendering methods built into Rails. How to create layouts with multiple...

we know that below is the right way to do it but my question is

From a ruby perspective what is ‘layout’ if it is not a method?

class ProductsController \< ApplicationController  
layout :products\_layout

def show  
@product = Product.find(params[:id])  
end

private def products\_layout  
@current\_user.special? ? “special” : “products”  
end

end

---

<div class="post-metadata">

**Author:** ![cristie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@cristie](https://www.ruby-forum.com/u/cristie)\
**Post date:** [November 20, 2009, 6:38pm UTC](https://www.ruby-forum.com/t/layout-declaration/179265/2 "2009-11-20T18:38:17Z")

</div>

Cris S. wrote:

> Hi all.
> 
> We have been trying to do a custom layout for special user’s via  
> something like this:
> 
> class ProductsController \< ApplicationController
> 
> def show  
> products\_layout  
> @product = Product.find(params[:id])  
> end
> 
> private def products\_layout  
> layout ‘special’ if @current\_user.special?  
> layout ‘not\_special’ unless @current\_user.special?  
> end
> 
> end
> 
> We got a ‘no such method error’ on layout.
> 
> via the url below:  
> [Layouts and Rendering in Rails — Ruby on Rails Guides](http://guides.rubyonrails.org/layouts_and_rendering.html)  
> we know that below is the right way to do it

Then do it that way. 🙂

> but my question is
> 
> From a ruby perspective what is ‘layout’ if it is not a method?

Of course it’s a method. It’s a class method in the controller. So to  
call it from within an instance method, you’d have to call  
self.class.layout.

> class ProductsController \< ApplicationController  
> layout :products\_layout
> 
> def show  
> @product = Product.find(params[:id])  
> end
> 
> private def products\_layout  
> @current\_user.special? ? “special” : “products”  
> end
> 
> end

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cristie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@cristie](https://www.ruby-forum.com/u/cristie)\
**Post date:** [November 21, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/layout-declaration/179265/3 "2009-11-21T17:15:00Z")

</div>

Thanks Marnen. I have replied to thank you three times and none of them  
seem to post…

Cris
