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).
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
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:
def determine_layout if params[:id].nil? return "fancy_layout" else return "default" end end end
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' end end
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.