Home arrow Ruby-on-Rails arrow Browsing and Searching an Online Book Catalog
RUBY-ON-RAILS

Browsing and Searching an Online Book Catalog


In this second part of a three-part series on building an online book catalog in Ruby-on-Rails, you'll learn how to set up the application to allow users to view book details and search for books. This article 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 18, 2010
TABLE OF CONTENTS:
  1. · Browsing and Searching an Online Book Catalog
  2. · Adding Links
  3. · Implementing the Search Books User Story
  4. · Updating the Integration Test

print this article
SEARCH DEVARTICLES

Browsing and Searching an Online Book Catalog
(Page 1 of 4 )

Implementing the View Book Details User Story

Having a catalog page for a series of books is nice, but its not suitable for excruciating details about every item. Therefore, the next thing for us to do is to implement a page for individual titles. As always, we start by writing a test for this story.

We already have a test case, so we can just extend that. Intest/integration/ browsing_and_searching_test.rb, well add another chapter to the story of Jill, right belowtest_browsing_the_site:

def test_getting_details
  jill = enter_site(:jill)
  jill.get_book_details_for "Pride and Prejudice"
end

Then we add a new method to ourBrowsingTestDSL module to keep the test code clean:

def get_book_details_for(title)
  @book = Book.find_by_title(title)
  get /catalog/show/#{@book.id}
  assert_response :success
  assert_template "catalog/show"

  assert_tag      :tag => "h1",
                  :content => @book.title 
  assert_tag      :tag => "h2",
                  :content => "by #{@book.authors.map{|a| a.name}}"
end

Theget_book_details_formethod simply fetches a book with the given name from the database, then requests the correspondingshowpage and checks that both the book title and the names of the authors are correctly displayed on the resulting page.

When we createdCatalogController, we specified that we want to have an action calledshowat hand. Therefore, we already have a stub methodshowinapp/controllers/catalog_controller.rband a pretty much empty view fileapp/views/catalog/show.rhtml. Lets now add some flesh around these bones.

Modifying the Controller

Implementing theshow action inCatalogControlleris a simple two-liner. Add the following toapp/controllers/catalog_controller.rb:

def show
  @book = Book.find(params[:id]) rescue nil
  return render(:text => "Not found", :status => 404)➥
unless @book
  @page_title = @book.title
end

All we do is to assign the@bookinstance variable with the book that matches theidwe get from the browser. If the book is not found, we show a very simple 404 Not Found page. Then we put the title of the book in the@page_titleinstance variable to make it show in the layout.

Modifying the View

In the view file, well show the details of the book at hand (remember that the book title is shown by the layout file inside anh1element). Add the following toapp/views/catalog/show.rhtml:

<h2>by <%= @book.authors.map{|a| a.name}.join(", ") %></h2>
<%= image_tag url_for_file_column(:book, :cover_image)➥
    unless @book.cover_image.blank? %>
<dl>
  <dt>Price</dt>
  <dd>$<%= sprintf("%0.2f", @book.price) -%></dd>
  <dt>Page count</dt>
  <dd><%= @book.page_count -%></dd> 
  <dt>Publisher</dt>
  <dd><%= @book.publisher.name %></dd>
  <dt>Blurb</dt>
  <dd><%= @book.blurb %></dd>
</dl>

<p><%= link_to "Back to Catalog", :action => "index" %></p>

Now the view will show the names of all the authors of a book separated by a comma. We also show the cover image of the book if one has been added, and other details of the book. We run the test again, and see that everything works just fine.

$ ruby test/integration/browsing_and_searching_test.rb

--------------------------------------------
Loaded suite test/integration/browsing_and_searching_test Started
..
Finished in 0.231862 seconds.

2 tests, 11 assertions, 0 failures, 0 errors
--------------------------------------------

Figure 4-1. shows a book detail page in action.


Figure 4-1.  Book detail page


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials