#7 All About Layouts
Mar 19, 2007 | 6 minutes |
Views
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.
- Download:
- mp4Full Size H.264 Video (12.1 MB)
- m4vSmaller H.264 Video (7.62 MB)
- webmFull Size VP8 Video (22.1 MB)
- ogvFull Size Theora Video (14.9 MB)
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)?
The convention is using a plural name for the controller (Projects).
Glad you find the videos useful. :)
It's really nice to have it all in one place. Now my code is a bit more DRY.
Thanks
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.
Jamal Soueidan
i think liquid is your demand
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?
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
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
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!
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
@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.
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.
Didn't realize layout names can be treated like methods. goodness!
This episode has been updated to Rails 5 as a blog post Layouts in Rails 5
This is one area that doesn't seem to have changed a whole heck of a lot from the original railscast to today! Will be very handy to use.