#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 18 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. kino May 23, 2008 at 01:54

The fact is evident that noetic acts become adjusted to, by conscious conversion into the corresponding cogitationes, separated modes of consciousness.


9. 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


10. 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


11. 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!


12. Andy Jun 13, 2008 at 16:23

Brilliant! Thanks so much.


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

Thanks Ryan!

Just the 'right' information and broken down perfectly.

Keep it up!


14. Buy Perfect World Gold Nov 20, 2008 at 17:45

Thanks Ryan,I think this is one of the most wonderful sites. I have great admiration for you.


15. Anarchy Online credits Nov 21, 2008 at 20:40

I have great admiration for you.


16. lily Nov 27, 2008 at 18:52

Thank you Ryan, your screencast is good. Please look at our URL, if necessary we can learn from each other.


17. evden eve nakliyat Dec 01, 2008 at 01:14

evden eve nakliyat


18. wakfu gold Dec 04, 2008 at 16:43

Thank you for your informations, it is wonderful! I have admiration for you.

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