What does this mean?

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.

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’

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

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…

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

b

On Apr 23, 4:53 am, Magicloud [email protected] 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.

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?