Home arrow Ruby-on-Rails arrow Page 4 - 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 - Running the Integration Test
(Page 4 of 4 )

Now that we have our simple browsing functionality implemented, we can run our test case.

$ ruby test/integration/browsing_and_searching_test.rb

Loaded suite test/integration/browsing_and_searching_test Started
Finished in 0.514885 seconds.

1 tests, 4 assertions, 0 failures, 0 errors

The test passes, but browsing is really not browsing if it involves only a single page. So, letís create another test case that checks that the pagination in our catalog works as expected. Make the following changes totest/integration/browsing_and_searching_test.rb:

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 go_to_second_page
      get "/catalog?page=2"
assert_response :success
assert_template "catalog/index"
assert_equal   Book.find_by_title("Pro Rails E-Commerce"),


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

Ingo_to_second_page, we first fetch the second catalog page. We then check that we get a normal response and the correct template in return. Finally, we check that the first one of the books in ourbooks.ymlfixture file is on this page, since the books are ordered in a descending chronological order on the catalog page. Running the tests again confirms that the catalog page is working as expected:

$ ruby test/integration/browsing_and_searching_test.rb

Loaded suite test/integration/browsing_and_searching_test Started
Finished in 0.110837 seconds.

1 tests, 7 assertions, 0 failures, 0 errors

Now that we have a working catalog page, it would be nice to make it the home page of the whole book store. We already briefly mentioned Rails routes in Chapter 2, and now weíre going to take advantage of them again. Openconfig/routes.rband change the line for default root url to look like this:

# You can have the root of your site routed by hooking up "
# -- just remember to delete public/index.html.
map.connect ", :controller => "catalog"

This means that all the requests for the root url are routed to the default action (index) ofCatalogController.

Please check back tomorrow for the second part of this article.

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