Home arrow Ruby-on-Rails arrow Page 2 - Building an Online Book Catalog

Building an Online Book Catalog

Now that we've built an online bookstore application in Ruby-on-Rails, it's time to serve our customers by building a book catalog of the store that they can use for browsing, viewing book details, searching for books, and finding out about new titles at a glance. This article, the first in a three-part series, is excerpted from chapter four 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 / 1
May 17, 2010
  1. · Building an Online Book Catalog
  2. · Implementing the Book Catalog Interface
  3. · Implementing the Browse Books User Story
  4. · Running the Integration Test

print this article

Building an Online Book Catalog - Implementing the Book Catalog Interface
(Page 2 of 4 )

To be able to really test browsing a list of titles, we need to have a number of books available for viewing. Therefore, we need to expand our authors.yml,publishers.yml,books.yml, andauthors_books.ymlfixture files intest/fixtures. You can download the files from the Source Code/Downloads section ofwww.apress.com.

As in the previous chapter, well use integration tests for this sprint, because they work well to exercise the book catalog browsing system from end to end. First, well create a test stub by using the Rails test generator:

$ script/generate integration_test BrowsingAndSearching

  exists  test/integration/

Again, well delete thetest_truthfrom the test file and replace it with our real test, as shown in Listing 4-1.

Listing 4-1. First Version of the Integration Test for the Book Catalog Interface

require "#{File.dirname(__FILE__)}/../test_helper"

class BrowsingAndSearchingTest < ActionController::IntegrationTest
  fixtures :publishers, :authors, :books, :authors_books

  def test_browsing_the_site
    jill = enter_site(:jill)


  module BrowsingTestDSL
    attr_writer :name

    def browse_index
      get "/catalog"

      assert_response :success
      assert_template "catalog/index"
      assert_tag      :tag => "dl", :attributes =>
{ :id => "books" },
                      :children =>
                        { :count => 10, :only =>
                          {:tag => "dt"}}
      assert_tag :tag => "dt", :content => "The Idiot"

  def enter_site(name)
open_session do |session|
      session.name = name
      yield session if block_given?

In the test case, we create a DSL module, as in the previous chapter. We first test that requesting"/catalog"works and that we get the defaultindex template rendered. Then we check that there is a list of books (laid out with an HTML definition list) on the page and that a book called The Idiot exists in that list.

Our test fails magnificently, in plain old TDD way, so its time to implement the functionality.

First, we need to create the controller for the catalog. Well name itcatalogso that it will match the URL requested in thebrowse_sitetest method. Once again, use the familiarscript/generatecommand and give the needed action names (for all our projected user stories) as parameters.

$ script/generate controller Catalog index show search latest

exists app/controllers/
exists app/helpers/
create app/views/catalog
exists test/functional/
create app/controllers/catalog_controller.rb
create test/functional/catalog_controller_test.rb
create app/helpers/catalog_helper.rb
create app/views/catalog/index.rhtml
create app/views/catalog/show.rhtml
create app/views/catalog/search.rhtml
create app/views/catalog/latest.rhtml










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