#7
Mar 19, 2007

All About Layouts

Everything you wanted to know about layouts: global layouts, controller layouts, shared layouts, dynamic layouts and action layouts. Yes, there really are that many ways to specify a layout.
Tags: views
Download (20.1 MB, 6:54)
alternative download for iPod & Apple TV (10.8 MB, 6:54)
# projects_controller.rb
layout :user_layout

def index
  @projects = Project.find(:all)
  render :layout => 'projects'
end

protected

def user_layout
  if current_user.admin?
    "admin"
  else
    "application"
  end
end

RSS Feed for Episode Comments 19 comments

1. jDeppen Jun 05, 2007 at 21:31

This is great! I am a new Mac user and I love that the key combinations flash in the top right as you type. These are great Rails training videos. Thanks

Should controller names be singular (Project) and rather than plural (Projects)?


2. Ryan Bates Jun 06, 2007 at 07:27

The convention is using a plural name for the controller (Projects).

Glad you find the videos useful. :)


3. Robertas Aganauskas Jun 22, 2007 at 05:55

It's really nice to have it all in one place. Now my code is a bit more DRY.

Thanks


4. Jamal Soueidan Sep 01, 2007 at 09:28

Thanks for those wonderful videos you are sharing with us :)

I would like to know how "dynamic layouts", I would like to add the layouts in my database and then use them as layouts, how can I accomplice that?

I tried to do it as you showed us in the video as follow:

def user_layout
  if current_user.admin?
 @layout.find_by_name("Normal").htmlCode
  else @layout.find_by_name("Default").htmlCode
  end

But this isn't working as I excepted, I hope you would have the time to give some example how to develop that :) and thanks in advance for your help.


5. andy Dec 07, 2007 at 23:11

 Jamal Soueidan

i think liquid is your demand


6. henry Dec 21, 2007 at 22:13

Nice screen cap, also if you use named spaced controllers, those child controllers inherit the layout of the parent class, very DRY.


7. Taylor Redden Jan 30, 2008 at 02:24

Hey Ryan, do you know of a way, to use the Application Layout for one section all the time (say header and footer) and then use different layouts in the yielded section.

should i just use partials?


8. BillNace May 24, 2008 at 01:55

Ryan,

  Thanks for the railscasts! Awesome!

  Could you post your application layout somewhere? It plus the CSS (which I grabbed from cast #58) make for a clean demo. But, I can't get the flash working right -- I always get a green and pink narrow band when the flash is empty. How do I make the flash_notice div go away when the flash is empty?

Thanks,
Bill


9. BillNace May 24, 2008 at 01:57

Ryan,

  Nevermind. Seconds after posting, I realized I could actually use Ruby, what a concept, in my layout! DOH!.
  I wrapped the divs in a <% unless flash[:notice].blank? %>

Thanks anyway,
Bill


10. nik Jun 12, 2008 at 09:02

Hello, I was doing a render layout thing, I found that for example if I put the render layout before any @abc = Abc.find(:all), this instance variable line won't work.
So failed is :
def translate
  render :layout =>'app'
  @pinyin = Pinyin.find(:all)
end
But succeeded is:
def translate
  @pinyin = Pinyin.find(:all)
  render :layout =>'app'
end

Does anyone know why this is?

Thanks a million!


11. Andy Jun 13, 2008 at 16:23

Brilliant! Thanks so much.


12. Danny de Wit Jun 15, 2008 at 01:35

Thanks Ryan!

Just the 'right' information and broken down perfectly.

Keep it up!


13. 找到组织了 May 22, 2009 at 02:41

找到组织了


14. Richard McIntyre Jun 25, 2009 at 16:07

Hello, I love your screencasts and you have a unique gift of breaking down potentially complicated concepts into very easy to understand chunks.

A REQUEST

Can you at some point address how to build more complicated layouts restfully. For example a products page in which a news feed is also fed in as the sub menu (which also appears on other pages) where would the view be fed with this data in a pure REST environment.

Many thanks


15. gw Sep 16, 2009 at 09:06

@Nic, the controller call render to create a response to send back to the browser, and any instance variables you require in the view must be set up first.


16. wl Sep 18, 2009 at 16:07

As henry mentioned, subclasses of a controller will use the parent's layout unless overridden. That explains why application.html.erb will be used as default if no other layout is specified.


17. ClumsyBird Dec 07, 2009 at 06:30

Didn't realize layout names can be treated like methods. goodness!


18. peter Jan 21, 2010 at 12:20

Thanks Ryan for this great episode.


19. hk_dresden Jan 23, 2010 at 10:08

Half an hour ago I find your episodes. I'm impressed.
Thanks!

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source