Home arrow Ruby-on-Rails arrow Page 4 - 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 - Adjusting the Flash Notifications
(Page 4 of 4 )

Browsing around the interface—creating, editing, and deleting authors—we can see that everything works. However, we find one shortcoming. The flash notifications we used to display messages to the user are not shown. Indeed, while we did assign the messages, we are never displaying them in the user interface.

Again, we create a failing test assertion demonstrating this flaw. We could put the new test in all the test methods that should be showing a notice usingflash, but we settle for doing it in the author deletion test.

def test_destroy
assert_difference(Author, :count, -1) do
    post :destroy, :id => 1
    assert_response :redirect
    assert_redirected_to :action => 'index'
    assert_tag :tag => 'div', :attributes => {:id => 'notice'},
          :content => 'Successfully deleted author Joel Spolsky'

follow_redirectis a Rails test helper method that causes the test case to follow aredirect_tocall in a controller. In our case, thedestroyaction redirected the user to theindexaction in the end, sofollow_redirectcauses theindexaction to be run. After that we can check that there is adivtag with the correct flash message on theindexpage.

Since flash messages can be shown to the user on many different pages, a natural place for displaying the notice is in the layout template used by all actions,app/views/layouts/application.rhtml. We add a bit of code there that will show the message stored inflash[:notice]if it is assigned.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="
    <title><%= @page_title || 'Emporium' %></title>
    <%= stylesheet_link_tag "style" %>
    <% if flash[:notice] %>
      <div id="notice">
        <%= flash[:notice] %>
    <% end %>

    <%= "<h1>#{@page_title}</h1>" if @page_title %>
    <%= yield %>

We also make a small change to the CSS file created in Chapter 1,public/stylesheets/style.css, adding a bit of color to make the notice stand out more on the page:

... a lot of CSS lines omitted ...
#notice {
  padding: 5px;
  background-color: #96FF88;

Running the test again indicates that the flash system works, and so does our empirical research done in the browser, as shown in Figure 2-5.

Figure 2-5.  Working flash message

Checking off the last thing as done from our sprint backlog, we call George to do the acceptance testing. George can’t believe we’re finished already. “Holy moly, I didn’t even have the time to finish my lunch yet,” he says, mustard dripping from his beard. Time spent for this sprint: three hours. Now that’s productivity!


In this chapter, we first introduced the approach of TDD and the testing methods Ruby on Rails supports. Then we continued with putting the TDD in action in Rails, creating a controller for handling adding, updating, deleting, and viewing authors. During the course of the chapter, we also briefly introduced ActiveRecord object-relational mapping, Rails migrations, and ActiveRecord validations. These techniques give you a solid foundation for managing the business logic of your Rails applications.

In the next chapter, you will learn that we could have done the same tasks we did in this chapter with a single command, by using the Rails scaffolding. But that’s okay. Doing it manually first, we could better introduce some useful Rails features and, most of all, run a code sprint in a true TDD fashion.

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