Home arrow Ruby-on-Rails arrow Page 3 - Managing Authors in an Online Bookstore
RUBY-ON-RAILS

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
TABLE OF CONTENTS:
  1. · Managing Authors in an Online Bookstore
  2. · Editing an Author
  3. · Deleting an Author
  4. · Adjusting the Flash Notifications

print this article
SEARCH DEVARTICLES

Managing Authors in an Online Bookstore - Deleting an Author
(Page 3 of 4 )

For the Delete Author user story, we simply want to test that posting a form to the destroyaction correctly deletes the author from the database and then redirects us to theindexpage. Letís openauthor_controller_test.rbagain and add another test case to it.  

def test_destroy
 
assert_difference(Author, :count, -1) do
    post :destroy, :id => 1
    assert_response :redirect
    assert_redirected_to :action => 'index'
  end
end

The test code is fairly simple, and the only thing new is that this time we pass-1as thedifferenceargument toassert_difference, in order to make sure the number of authors decreases by one as the result of thedestroy action.

Implementing thedestroyaction is straightforward. We donít need a view for it, since the action just deletes the author and redirects back to the author list.

def destroy
  @author = Author.find(params[:id])
  flash[:notice] = "Successfully deleted author #{@author.name}"
  @author.destroy
  redirect_to :action => 'index'
end

Thatís all the code it takes to delete an author. We again fetch the right author from the database using thefindmethod, and then use the ActiveRecorddestroymethod to delete the author from the database. In between, we populate the flash notice so that theindexpage where we redirect the user will show a notification to the user about the successful deletion.

We can now run the whole test enchilada.

$ ruby test/functional/admin/author_controller_test.rb

--------------------------------------------
Loaded suite test/functional/admin/author_controller_test Started
......
Finished in 0.176094 seconds.

6 tests, 31 assertions, 0 failures, 0 errors
--------------------------------------------

The result is 31 assertions, no failures, no errors. It works! It works!!! We exchange a couple of high-fives before we start the WEBrick server again, and point the browser tohttp://localhost:3000/admin/author/. The result is shown in Figure 2-4.


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