Home arrow Ruby-on-Rails arrow Validating Author Data in an Online Bookstore

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

print this article

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

Creating the Form

Now we need to make the test pass. Open the controller file,app/controllers/admin/ author_controller.rb. You can see that the file has a method for each of the actions we specified in thegenerate controllercommand. We’re now interested in thenewaction, which outputs the form for creating a new author. All we need in thenewaction is a newAuthorobject. We also set the@page_titlevariable (used by the default layout file described in Chapter 1) to something meaningful.

class Admin::AuthorController < ApplicationController
  def new
    @author = Author.new
@page_title = 'Create new author'

  def create

  def edit

  def update

  def destroy

  def show

  def index

Next, we need to create the view for thenewaction. Openapp/views/admin/author/new.rhtmland add the following template code to it:

<%= form_tag :action => 'create' %>
<%= render :partial => 'form' %>
<%= submit_tag 'Create' %>
<%= end_form_tag %>

<%= link_to 'Back', :action => 'index' %>

Tip  Usingrender :partial => 'partial_name'is a great way to avoid code duplication in Rails templates. The method call finds a template file_partial_name.rhtmland renders it as a part of the surrounding template. This way we can, for example, use the same partial form template for both creating a new author and editing an existing author. If used together with a:collectionoption, the call will render the template once for each element of the container passed with the option. You will see an example of this in the “Listing Authors” section later in this chapter, where we output a similar table row for each author we have in the system. More information about rendering partials in Rails can be found in the Rails API docs athttp://api.rubyonrails.org/classes/ActionController/ Base.html#M000206.

The template first creates a<form>tag with theform_taghelper, pointing it to thecreateaction. Then it renders the actual form, which we will implement in a minute using a partial template. Next, our template outputs a submit button for the form and closes it. Finally, we show a link to the index page listing all the authors (although we haven’t implemented that page yet). Next, we need to create the partial template for the actual form. Create a new fileapp/views/admin/author/_form.rhtmland add the following form to it:

<%= error_messages_for 'author' %>

<p><label for="author_first_name">First name</label><br/>
<%= text_field 'author', 'first_name' %></p>

<p><label for="author_last_name">Last name</label><br/>
<%= text_field 'author', 'last_name' %></p>

In the form, we use two useful Rails helper methods:

  1. error_messages_foroutputs the validation error notification if there are problems with the form input. 
  2. text_fieldis a form helper that creates a text field element for a given object attribute—in this case, author’s first and last names. The real value oftext_fieldcan be seen when a user inputs something invalid to the form. Rails will automatically mark the field with CSS classfieldWithErrors, so you can use CSS to make the field stand out from other fields.

If we run our functional test again, we can see it pass. Openinghttp://localhost:3000/ admin/author/newin a browser, we can see that we have implemented the first part of adding a new author, as shown in Figure 2-1.

Figure 2-1.  Form for creating a new author

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