Home arrow Ruby-on-Rails arrow Page 2 - 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 - Editing an Author
(Page 2 of 4 )

We start our test case for the Edit Author user story in the same way we did with the test case for the create functionality, by loading the form page.

def test_edit
  get :edit, :id => 1
  assert_tag  :tag => 'input',
              :attributes => { :name => 'author[first_name]',
                               :value => 'Joel' }
  assert_tag  :tag => 'input',
              :attributes => { :name => 'author[last_name]',
                               :value => 'Spolsky' }

We try to load the edit page for author 1 and test that the form has input fields with correct preset values. After running the test and finding it running red, we implement the edit interface. We first set the needed values in theeditaction inapp/controllers/admin/author_controller.rb:

def edit
@author = Author.find(params[:id])
  @page_title = 'Edit author'

We use the normalActiveRecord findmethod to fetch theAuthorobject with theidthat was passed with the URL. Then we populate the page title, just as we did with thenewpage. The view fileapp/views/admin/author/edit.rhtmllooks pretty much the same asnew.rhtml, with the exception that the form action isupdateinstead ofcreatethis time. Note that we use the same partial template to output the actual form fields as on thenewpage.

<%= start_form_tag :action => 'update', :id => @author %>
<%= render :partial => 'form' %>
<%= submit_tag 'Edit' %>
<%= end_form_tag %>

<%= link_to 'Show', :action => 'show', :id => @author %> |
<%= link_to 'Back', :action => 'index' %>

We run the tests again, and everything should pass. Next, we need to implement the action that receives the edit form post,update. We extend our tests a bit:

def test_update
post :update, :id => 1, :author => { :first_name => 'Joseph',
:last_name => 'Spolsky' }
assert_response :redirect
assert_redirected_to :action => 'show', :id => 1
assert_equal 'Joseph', Author.find(1).first_name

In the update, we post the form to theupdateaction, and after that, check that we are redirected and, more important, that the first name of the author is really changed. The test doesnít yet pass, of course, so letís open theauthor_controller.rbfile again and implement theupdate action.

def update
@author = Author.find(params[:id])
if @author.update_attributes(params[:author])
flash[:notice] = 'Author was successfully updated.'
redirect_to :action => 'show', :id => @author
@page_title = 'Edit author'
render :action => 'edit'

You can see that the action is more or less similar to thecreateaction, with the distinction that, this time, we use theupdate_attributesmethod to update the@authorobject. It updates the attributes of the object with the values it gets from the form parameters, and after that, it callssaveimplicitly. Just likesave, it returnsfalseif saving the object didnít succeed.

We can now run the test file again, and see that everything passes just fine.

$ ruby test/functional/admin/author_controller_test.rb

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

3 tests, 28 assertions, 0 failures, 0 errors

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