Home arrow Ruby-on-Rails arrow Ruby on Rails Templates and Layouts
RUBY-ON-RAILS

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
TABLE OF CONTENTS:
  1. · Ruby on Rails Templates and Layouts
  2. · Working with Ruby-on-Rails Layouts

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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