# What does this mean?

**URL:** <https://www.ruby-forum.com/t/what-does-this-mean/92712>\
**Category:** Rails\
**Created:** [April 23, 2007, 10:54am UTC](https://www.ruby-forum.com/t/what-does-this-mean/92712 "2007-04-23T10:54:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Magicloud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Magicloud](https://www.ruby-forum.com/u/Magicloud)\
**Post date:** [April 23, 2007, 10:54am UTC](https://www.ruby-forum.com/t/what-does-this-mean/92712/1 "2007-04-23T10:54:52Z")

</div>

Holds the template files for layouts to be used with views. This  
models the common  
header/footer method of wrapping views. In your views, define a  
layout using the  
layout :default and create a file named default.rhtml.  
Inside default.rhtml,  
call \<% yield %\> to render the view using this layout.

This text is from official readme. I do not know where to write  
“layout :default”, and where to put default.rhtml file…  
I used to put “layout :default” in application.rb, so all page use  
this layout, but now, it does not work…

Thanks.

---

<div class="post-metadata">

**Author:** ![Magicloud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Magicloud](https://www.ruby-forum.com/u/Magicloud)\
**Post date:** [April 23, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/what-does-this-mean/92712/2 "2007-04-23T17:25:31Z")

</div>

> I used to put “layout :default” in application.rb, so all page use  
> this layout, but now, it does not work…

Create: app/views/layouts/default.rhtml…

Then in app/controllers/application.rb put b/n the class…end block.

layout ‘default’

---

<div class="post-metadata">

**Author:** ![Magicloud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Magicloud](https://www.ruby-forum.com/u/Magicloud)\
**Post date:** [April 23, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/what-does-this-mean/92712/3 "2007-04-23T20:37:40Z")

</div>

Philip H. wrote:

> > I used to put “layout :default” in application.rb, so all page use  
> > this layout, but now, it does not work…
> 
> Create: app/views/layouts/default.rhtml…
> 
> Then in app/controllers/application.rb put b/n the class…end block.
> 
> layout ‘default’

However you only need to do that if you want your layout to be called  
“default” for some reason. If you call your layout rhtml file  
application.rhtml, then that layout will be used automatically… no  
need to specify it in application.rb.

If you need different layouts for different parts of your app, you can  
make additional layout files and using the “layout ‘layoutname’” call in  
the appropriate controller… or name the layout file the same name as  
your controller (as scaffolding does) and it will get used automatically  
for calls to that controller.

b

---

<div class="post-metadata">

**Author:** ![Magicloud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Magicloud](https://www.ruby-forum.com/u/Magicloud)\
**Post date:** [April 24, 2007, 3:30am UTC](https://www.ruby-forum.com/t/what-does-this-mean/92712/4 "2007-04-24T03:30:18Z")

</div>

It seems that it does not work, i make a app/views/layouts/  
application.rhtml file, and it does not work as a default layout…

---

<div class="post-metadata">

**Author:** ![Magicloud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Magicloud](https://www.ruby-forum.com/u/Magicloud)\
**Post date:** [April 24, 2007, 6:07am UTC](https://www.ruby-forum.com/t/what-does-this-mean/92712/5 "2007-04-24T06:07:27Z")

</div>

Hmm, works for me… Are you sure you haven’t set a “layout ‘something’”  
in the controller you’re calling?

b

---

<div class="post-metadata">

**Author:** ![Magicloud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Magicloud](https://www.ruby-forum.com/u/Magicloud)\
**Post date:** [April 28, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/what-does-this-mean/92712/6 "2007-04-28T16:37:27Z")

</div>

On Apr 23, 4:53 am, Magicloud [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This text is from official readme. I do not know where to write  
> “layout :default”, and where to put default.rhtml file…

You can specify the layout in your controller. If you want to have a  
default layout, it’s best to name it “application.rhtml” (or .rxml, or  
whichever). Rails will use that as the default for any controller that  
doesn’t have its own layout. Layouts go in “your\_application/app/views/  
layouts” by convention, and Rails will look for them there.

So, how does a controller get its own layout? Two ways. The most  
common is simply by naming a layout after the controller. For example,  
if you have a controller called “FooController”, then Rails will look  
for a layout called “foo.rhtml” (or .rxml, or whichever) first.

The second way is to explicitly name the layout you want applied to  
the views of a controller within the controller itself (this is where  
“layout :default” comes in). Say you want FooController to have the  
“bar” layout instead:

class FooController \< ApplicationController  
layout “bar”  
. . .  
end

Layouts are inherited, so if FoobarController extends FooController,  
it will also get the “bar” layout unless you specify otherwise.  
Finally, the “layout” declaration can actually take arguments beyond  
just strings, so you can conditionally apply layouts and do all sorts  
of other craziness if that sort of thing turns you on.

---

<div class="post-metadata">

**Author:** ![Magicloud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Magicloud](https://www.ruby-forum.com/u/Magicloud)\
**Post date:** [April 28, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/what-does-this-mean/92712/7 "2007-04-28T19:44:39Z")

</div>

I am trying to use an inherited hook:

module StarMonkey  
class MyStarMonkey  
def self.inherited(klass)  
puts klass.const\_defined(‘LIKES\_CRUNCHY\_BACON’)  
end  
end  
end

My personal star monkey

class FredrickTheGreat \< StarMonkey::MyStarMonkey  
LIKES\_CRUNCHY\_BACON = ‘hello world!’  
end

now for IRB goodness:

> > puts FredrickTheGreat.const\_defined?(‘LIKES\_CRUNCY\_BACON’)  
> > =\> true  
> > =\> true

seems to work. now for something more real life

The hook

module ActiveRecord  
class Base  
def slef.inherited(klass)  
puts klass.has\_const(‘LIKES\_CRUNCHY\_BACON’)  
end  
end  
end

Model

class StarMonkey \< ActiveRecord::Base  
LIKES\_CRUNCHY\_BACON = true  
end

script/console —

> > puts StarMonkey.const\_defined?(‘LIKES\_CRUNCHY\_BACON’)  
> > =\>false #this is my inherited hook  
> > =\>true

What gives?
