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).
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' end
def create end
def edit end
def update end
def destroy end
def show end
def index end end
Next, we need to create the view for thenewaction. Openapp/views/admin/author/new.rhtmland add the following template code to it:
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:
In the form, we use two useful Rails helper methods:
error_messages_foroutputs the validation error notification if there are problems with the form input.
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.