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).
Ruby on Rails Templates and Layouts (Page 1 of 2 )
Creating a Template
Our next step is to create a template for our action to render. Create a new file in app/views/userscallednew.rhtmland edit it so it looks like this:
<h2>I am the new template!</h2>
Make sure your local web server is running (./script/server), and openhttp:// localhost:3000in your browser. Assuming everything is firing correctly, youíll see your new page:
-------------------------------------------- I am the new template! --------------------------------------------
Itís not very pretty, but it works. And it demonstrates the most basic of Action Packís conventions at work: templates are organized by controller name, and a template with the same name as the action being invoked will be rendered automatically. You didnít need to wire up anything. Merely by requesting thenewaction on theuserscontroller, Rails rendered the new template in theapp/views/usersdirectory.
After determining which controller to invoke, Rails proceeds to instantiate it and call itsnewmethod. Its default response after running thenewaction is to perform a render. Rails looks for a template namednew.rhtmlin theapp/views/usersdirectory and loads it.
At this point, the request cycle, while not very eventful from the browserís perspective, is complete. If you refresh your browser, the cycle begins anew and the same result is rendered. Notice how all the internals are already taken care of for you. All you need to do is create an appropriately named controller, action, and view, stick them in the right place, and request the URL in your browser. Rails takes care of making sure everything gets knitted together properly.
Note In edge-Rails, the.rhtmlextension is deprecated in favor of.erb(for Embedded Ruby, or ERb). This isnít the case for the most recent stable version of Rails at the time of this writing (1.2.3), but itís something to keep an eye out for in the future. The.rhtmlextension will still be supported for a long while and isnít scheduled to officially disappear until Rails 2.0.
While weíre kicking the tires, letís see what happens if we comment out thenewaction, like this:
class UsersController < ApplicationController #def new # @user = User.new #end end
If you refresh your browser, youíll notice that nothing has changed. No, thatís not because the web server needs to be restarted, nor is it because the page is cached. As it turns out, even if an action is missing, Rails will still look to see if it can find a template named for the sought-after action and render it if found. After all, sometimes you need to render a template but you donít have any logic to perform.
Before we go any further, use your browserís view source command to see the HTML that was produced. Now, if you know anything about HTML (and chances are you do), youíll quickly realize that weíre lacking something here: the<html>tags. Sure, you see your content, but whereís the layout? Most web pages have headers, footers, sidebars, and other page elements that, when styled, make the page look pretty. Fortunately, Rails has a built-in facility for dealing with page layouts.