Home arrow Ruby-on-Rails arrow Page 5 - Testing a Book Emporium Application

Testing a Book Emporium Application

In this conclusion to a three-part series on how to do a project setup and proof of concept for a fictional bookstore on Ruby-on-Rails, we'll actually start the application for the first time and see how it functions. This article is excerpted from chapter one of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
April 30, 2010
  1. · Testing a Book Emporium Application
  2. · Starting Emporium for the First Time
  3. · How Does Ruby on Rails Work?
  4. · Modifying the Generated View
  5. · Creating the Layout
  6. · Creating a Style Sheet

print this article

Testing a Book Emporium Application - Creating the Layout
(Page 5 of 6 )

George is a bit happier now, but he says that the page is not as nice looking as the current Emporium website, which he tells us was designed eight and a half years ago by his then ten-year-old nephew. He agrees that Emporium needs a new site design, but he tells us that he just sent some money to his starving sister on the Cayman Islands. So we decide to implement a design that can be improved later, because it will take a month or two before George can afford a professional designer.

Layouts are used in Ruby on Rails for surrounding the content of your pages with a header and footer. Figure 1-5 illustrates the concept of Rails layouts and views. The example shows a typical page consisting of a header, content, and footer section.

Figure 1-5.  Layouts and views

The same result can also be achieved by inserting the same header and footer code in all views, but this goes against the Don’t Repeat Yourself (DRY) principle, which states that you should avoid code duplication in all parts of your code.

Consider the following HTML for a page generated by Rails.

<!--Content start-->
Page content
<!--Content end-->

All content above the text<!--Content start-->and below<!--Content end-->comes from the following layout file. The view contains the text “Page content.”

<!--Content start-->
<%= yield %>
<!--Content end-->

Listing 1-2 shows a very minimalist layout, which is enough for the proof of concept. Enter it in an editor and save the contents inapp/views/layouts/application.rhtml.

Listing 1-2. Emporium’s First Layout

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<title><%= @page_title || 'Emporium' %></title>
<%= stylesheet_link_tag "style" %>
    <%= "<h1>#{@page_title}</h1>" if @page_title %>
<%= yield %>

The layout contains four Ruby expressions that are all evaluated when the page is rendered. The first expression will allow us to set the page title by defining an instance variable in our controllers:

<%= @page_title || 'Emporium' %>

The default title, “Emporium,” is used if we don’t specify the@page_titlevariable in the controller.

The second expression includes the Emporium style sheet, which we’ll create in the next section.

Tip  Thestylesheet_link_tagautomatically appends the last modification date to the URL of the style sheet file, for example,/stylesheets/style.css?1150321221. Appending the modification date will make the browser download the style sheet again, when it is updated, instead of taking a stale one from the browser cache. The same logic is used for thejavascript_include_tag.

The third expression allows us to use the title of the page as the page heading:

<%= "<h1>@page_title</h1>" if @page_title %>

If the instance variable@page_titlehas not been defined in the controller, then nothing is shown.

The last expression inserts the view part of the page by a call to theyieldmethod.

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-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials