Home arrow Ruby-on-Rails arrow Managing Authors in an Online Bookstore

Managing Authors in an Online Bookstore

In this conclusion to a four-part series on building an online bookstore application in Ruby-on-Rails, you'll learn how to edit and delete authors, 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 / 2
May 14, 2010
  1. · Managing Authors in an Online Bookstore
  2. · Editing an Author
  3. · Deleting an Author
  4. · Adjusting the Flash Notifications

print this article

Managing Authors in an Online Bookstore
(Page 1 of 4 )

Viewing an Author

As you might have noticed, creating the Author model also created a file calledauthors.ymlintest/fixtures. This is called a fixture file. Fixtures are mock data that can be used to populate the database with consistent data before each test method. Since the test database is purged before every test method, you know that all the data that exists in the database at that point came from the fixture files.  

It would be handy to have a few authors in the database for testing our view functionality, so we go ahead and create a couple of author fixtures inauthors.yml:

  id: 1
  first_name: Joel
  last_name: Spolsky
  id: 2
  first_name: Jeremy
  last_name: Keith

Putting the linefixtures :authorsin the beginning of our functional test class makes Rails load the author fixtures automatically before every test method inside that class:

class Admin::AuthorControllerTest < Test::Unit::TestCase
fixtures :authors

Now we can rest assured that when we start testing viewing an author, we have two items in ourauthors table.

We’ll keep the show author page very simple. We just want to make sure that we’re fed the right template and that the author is the one we’re expecting. Add the following test case to the bottom ofauthor_controller_test.rb:

def test_show
get :show, :id => 1
assert_template 'admin/author/show'
assert_equal 'Joel', assigns(:author).first_name
assert_equal 'Spolsky', assigns(:author).last_name

Here, we simply request the show page for one of our fixture authors and check that we get the correct template. Then we use theassigns helper to check that the author instance variable assigned in the action is the one it should be.assignsis a test helper method that can be used to access all the instance variables set in the last requested action. Here, we expect that theshowaction assigns a variable@authorand that the variable responds to the methodsfirst_nameandlast_name, returning “Joel” and “Spolsky,” respectively.

The controller code for theshowaction is simple. We fetch the author from the database and set the page title to the author’s name.

def show
@author = Author.find(params[:id])
  @page_title = @author.name

Now let’s open the view file,app/views/admin/author/show.rhtml, and add the template code:

<dt>First Name</dt>
<dd><%= @author.first_name %></dd>
<dt>Last Name</dt>
<dd><%= @author.last_name %></dd>

<p><%= link_to 'Edit', :action => 'edit', :id => @author %> |
<%= link_to 'Back', :action => 'index' %></p>

We run the tests again, and seeing them passing, open the browser and check that the page looks fine there, too, as shown in Figure 2-3.

Figure 2-3.  Show author page

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