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).
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:
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"), assigns(:books).last end end
def enter_site(name) open_session do |session| session.extend(BrowsingTestDSL) session.name = name yield session if block_given? end end end
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:
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.