Home arrow Ruby-on-Rails arrow Page 3 - Validating Author Data in an Online Bookstore
RUBY-ON-RAILS

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
TABLE OF CONTENTS:
  1. · Validating Author Data in an Online Bookstore
  2. · Creating the Author
  3. · Validating Data
  4. · Listing Authors

print this article
SEARCH DEVARTICLES

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

ActiveRecord validations are a way to enforce restrictions upon editing the business model objects of your application. For example, you might want to make sure that an e-mail address is valid or that the balance of an account can never be less than zero.

In our case, we just want to make sure that every time someone tries to add or update anAuthorobject, that object has both the first and the last name specified. This can be accomplished with thevalidates_presence_ofmethod, so letís openapp/models/author.rband add a line of code there to make the validations work:

class Author < ActiveRecord::Base
 
validates_presence_of :first_name, :last_name

  def name
    "#{first_name} #{last_name}"
  end
end

We will meet a lot more validations during the course of this book, but for now, letís just settle with this and try to run our tests again.

$ ruby test/functional/admin/author_controller_test.rb

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

2 tests, 12 assertions, 0 failures, 0 errors
--------------------------------------------

The results look good. We can now point our browser tohttp://localhost:3000/admin/author/newand try to create a new author. Leaving the last name blank, weíre greeted with the response shown in Figure 2-2 when the form is submitted.


Figure 2-2.  Failing creation of a new author

Seems that our validations are working, and as the tests agree, we move on to implement editing of existing authors.


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