Home arrow Ruby-on-Rails arrow Page 6 - 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 a Style Sheet
(Page 6 of 6 )

The About page looks a bit plain. It’s using the browser’s default font and font sizes. To make the page look nicer, we’ll tell Emporium to use a style sheet. Style sheets separate presentation from content and allow you to define, for example, the font and colors of HTML elements. The biggest benefit of using style sheets is that it allows us to separate content from presentation. This allows us to change the whole look and feel of our site by modifying only the style sheet.

Tip  For more information about style sheets, see the World Wide Web Consortium’s page on Cascading Style Sheets:www.w3.org/Style/CSS/.

The standard way of including style sheets in Rails is through thestylesheet_link_tag, as shown in Listing 1-3.

Listing 1-3. The Initial Version of Emporium’s Style Sheet

body { background-color: #fff; color: #333; }

body, p, ol, ul, td {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size:   13px;
  line-height: 18px;

pre {
  background-color: #eee;
  padding: 10px;
  font-size: 11px;

a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

Note that we have included only an excerpt from the style sheet; the complete style sheet can be downloaded from the Source Code section of the Apress website (www.apress.com). Save the style sheet inpublic/stylesheets/style.css.

Using Multiple Layouts

An application can have many layouts; for example, it may have one for normal pages and one for pop-up windows. You can tell Ruby on Rails to use a specific layout in many ways, of which three are shown here:

class EternalLifeController < ApplicationController
# Option 1
  layout 'default'
  # Option 2
  layout :determine_layout

  def index
    # Uses app/views/layouts/default.rhtml 

  def popup
    # Uses app/views/layouts/popup.rhtml
# Option 3
    render :layout => 'popup'

  def determine_layout
    if params[:id].nil?
      return "fancy_layout"
      return "default"

Options 1 and 2 are self-explanatory. Option 2 uses a method that decides which layout to use based on some runtime information; in this case, it checks if theidparameter is null and uses thefancy_layoutin that case.

The easiest way of changing the layout is by creating a file namedapplication.rhtml. This is the default layout file and will be used by Rails without the need for manually specifying the layout.

Modifying the Generated Controller

The last thing we need to do to complete the proof of concept is to modify the controller and action. The controller is where the main logic of your application is located. Each controller has one or more actions that execute the business logic.

The Ruby on Railsgenerate script already created a controller for us inapp/controllers/. Change it as follows. Note that we set the page title to “About Emporium.”

class AboutController < ApplicationController
  def index
    @page_title = 'About Emporium'

George is still standing behind our backs. He yells, “I’ve been standing here for 15 minutes, guys. I don’t have the whole day! Where’s my proof of concept?” We again reload the page in the browser, and he can finally see his proof of concept—a working About page showing a brief description and Emporium’s address, as shown in Figure 1-6.

Figure 1-6.  The completed proof of concept


In this chapter, we showed you how to implement a simple proof of concept for the Emporium project. We first explained how to install Ruby, Ruby on Rails, MySQL, and the MySQL driver. Then we showed you how to create a Ruby on Rails project skeleton using the railscommand. Next, we introduced you to controllers, views, and layouts, which we used for implementing the About Emporium user story.  

In the next chapter, we’ll implement the user stories related to author management and introduce you to concepts like Test Driven Development (TDD) and ActiveRecord.  


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