Home arrow Ruby-on-Rails arrow Page 4 - Validating Author Data in an Online Bookstore

Validating Author Data in an Online Bookstore

In this third part of a four-part article series on building an online bookstore application with Ruby on Rails, you'll learn how to actually create authors in the system, validate data, and more. This article is excerpted from chapter two 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 13, 2010
  1. · Validating Author Data in an Online Bookstore
  2. · Creating the Author
  3. · Validating Data
  4. · Listing Authors

print this article

Validating Author Data in an Online Bookstore - Listing Authors
(Page 4 of 4 )

Now that we are able to create authors, it would be nice to be able to view and list the authors in the system, too. We will next implement the author list page, which we’ll make the indexaction of our controller.  

At its simplest, we want our author list to show a table with rows for all authors and a header row. Let’s create a test case for it inauthor_controller_test.rb.

def test_index
  get :index
  assert_response :success
  assert_tag :tag => 'table',
:children => { :count => Author.count + 1,
                            :only => {:tag => 'tr'} }
  Author.find(:all).each do |a|
    assert_tag  :tag => 'td',
                :content => a.first_name
    assert_tag  :tag => 'td',
                :content => a.last_name

In the test, we first check that theindexaction returns a successful HTTP response. Then we check that there is a table with rows for each author in the database and a header row on the resulting page. We do this by using the:childrenand:countoptions ofassert_tag. We also check that there are table cells holding the names of all authors in the database.

Implementing theindexaction is straightforward. In the controller, we just fetch all the authors from the database and set the page title:

def index
  @authors = Author.find(:all)
  @page_title = 'Listing authors' 

In the view file,app/views/admin/author/index.rhtml, we then display the table and, at the bottom of the page, a link to add a new author:


  <%= render :partial => 'author', :collection => @authors %>

<p><%= link_to 'Add a new author', :action => 'new' %></p>

This time, we use the:collectionoption in therendercall to render the_author.rhtmlpartial template once for each author in@authors. Let’s create the partial template (app/views/ admin/author/_author.rhtml) and make it show a table row for each author.

  <td><%= link_to author.name, :action => 'show', :id => author %></td>
  <td><%= link_to 'Edit', :action => 'edit', :id => author %></td>
<%= button_to 'Delete', {:action => 'destroy', :id => author},
        :confirm =>
          "Are you sure you want to delete author #{author.name}?" %>

On each row, we show the name of the author linking to an individual show page (which we’ll implement soon) and a link to edit the author details. In the last cell, we use thebutton_tohelper to show a form button for deleting the author record (we will implement the action for deleting authors later in this chapter). We pass the method call a:confirmoption, which causes the browser to ask the user for a confirmation with JavaScript when the Delete button is clicked.

We can now run the test file again and see that everything seems to be in order. Now we will move on to the next user story, viewing the details of an individual author.

Please check back tomorrow for the conclusion to this series.

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