Home arrow Ruby-on-Rails arrow Page 2 - 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 - Adding Links
(Page 2 of 4 )

Now that we have pages for individual books, it would be a good idea to link to them from the catalog list page. Letís make sure that a link exists for each book on the catalog/index page. We create a separate method for checking the links, and then call that method from both the browse_indexandgo_to_second_pagemethods. Add the following new method and calls for it to theBrowsingTestDSLmodule intest/integration/browsing_and_searching_test.rb:

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"
   
check_book_links
 
end

  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
   
check_book_links
 
end

  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

  def check_book_links
    for book in assigns(:books)
      assert_tag    :tag => "a", :attributes =>
                      { :href => "/catalog/show/#{book.id}"}
    end
  end

end

The next thing to do is to create the links on the index page. Openapp/views/catalog/index.rhtmland add the highlighted code:

<dl id="books">
  <% for book in @books %>
    <dt><%= link_to book.title, :action => "show", :id => book %></dt>
    <% for author in book.authors %>
      <dd><%= author.last_name %>, <%= author.first_name %></dd>
    <% end %>
      <dd><%= pluralize(book.page_count, "page") %></dd>
      <dd>Price: $<%= sprintf("%.2f", book.price) %></dd>
      <dd><small>Publisher: <%= book.publisher.name %></small></dd>
  <% end %> 
</dl>

Run the tests again. See for yourself the results in Figure 4-2, and bathe in the glory of having implemented yet another user story.


Figure 4-2.  Catalog list page with links


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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials