Home arrow Ruby-on-Rails arrow Page 2 - Ruby on Rails Templates and Layouts

Ruby on Rails Templates and Layouts

In this sixth part to a multi-part series on the Action Pack library for Rails, you'll learn how to create a template and work with layouts. This article is excerpted from chapter six of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
March 30, 2011
  1. · Ruby on Rails Templates and Layouts
  2. · Working with Ruby-on-Rails Layouts

print this article

Ruby on Rails Templates and Layouts - Working with Ruby-on-Rails Layouts
(Page 2 of 2 )

Rails uses layouts to interpolate the output of an individual template into a larger whole—a reversal of the common pattern of including a shared header and footer on every page (which if you’ve done any work in other languages like PHP or ASP will be all too familiar). The scaffold generator we ran in Chapter 3 created a layout file and placed it in app/views/layouts/events.rhtml. It’s named after the controller we generated (events), and will be applied automatically to that controller only. That’s the way it works in Rails. Just as an action tries to render itself using a view that matches its name, so does a controller attempt to use a layout that matches its name. However, we’d like our layout to apply to all our controllers, including the one we’re working on now.

To have it apply to all our controllers, we can rename it toapplication.rhtml. Go ahead and do that now, and when you’re finished, open it in your editor. You should see something like the file shown in Listing 6-3.

Listing 6-3. The app/views/layouts/application.rhtml File

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <title>Events: <%= controller.action_name %></title>
    <%= stylesheet_link_tag 'scaffold' %>

    <p style="color: green"><%= flash[:notice] %></p>
    <%= yield %>

At rendering time, the layout will yield the results of the template fragment’s execution in place. See the<%= yield %>bit that we’ve highlighted in bold? That’s the important part. Wherever you put theyield keyword is where your content will go.

One more thing to note here: Rails is all about convention over configuration. Here, the convention is that a layout with the nameapplication.rhtmlis automatically applied to all templates unless an alternate is specified in the controller. This means that if you were to change the name of the layout as it stands, it wouldn’t be automatically applied. If you wanted to apply a different layout to a given controller, you could specify it in the controller itself using the class method,layout.

class ExampleController < ApplicationController
layout 'my_layout' # Will look for a layout in app/views/layouts/my_layout.rhtml end


A few conventions apply to working with layouts:

  • A layout named application.rhtml will be applied automatically unless a more specific candidate
    exists or is explicitly specified in the controller.
  • A layout that matches the name of a controller will be automatically applied if present. Controller-specific layouts take precedence over the application-level layout.
  • You can use the layout directive at the class level in any controller (that is, not inside an action) to set the layout for the entire controller: layout 'my_layout'.
  • You can include a layout for a specific action with an explicit call to render inside the action:
    render :layout => 'my_layout'.
  • Sometimes you want to render an action without a layout. In that case, you can pass false in place of the layout name: render :layout => false.

In practice, we usually just use application.rhtml and rarely take advantage of the controllerspecific
layout functionality. On the occasions when we need to use a different layout for a particular controller, we use the layout directive.

Please check back for the next part of the series.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials