August 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Avoiding Data Corruption with Rails' Active Record Validations

  • November 12, 2007
  • By Jason Gilmore
  • Send Email »
  • More Articles »

Rendered to the browser, the support form looks like this:

Figure 1: The customer support form

Finally, implementing the submit method in its simplest form can be done in just a few lines:

def submit
   @question = Question.new(params[:question])
   if @question.save
      flash[:notice] = "Your question has been successfully sent to
                        customer service.
                        Would you like to submit another question?"
      render :action => "index"
   end
end

Although the support form is now functional, with no controls in place for validating input, chances are your support team will soon be greeted with support requests replete with blank names and messages, invalid email addresses, and incorrect phone numbers. The result? Angry customers! However, you can use Active Record's validation features to avoid such problems altogether.

Active Record's Validation Methods

Rails' Active Record validation features go a long way towards automating some of the most commonplace validation tasks, such as checking for the presence of a data field, determining string length, ensuring uniqueness, and even validating a string according to rules as defined by a regular expression. Setting the desired validation rules is as simple as modifying the model, as you'll soon see.

Validating Presence

Logically, you want to make sure the user completes the name, phone number, email address, and message fields. To do so, use the validates_presence_of method. To make sure these fields are not blank, modify the Question model so it looks like this:

class Question < ActiveRecord::Base
   validates_presence_of :name,    :message=>"can't be blank!"
   validates_presence_of :email,   :message=>"can't be blank!"
   validates_presence_of :phone,   :message=>"can't be blank!"
   validates_presence_of :message, :message=>"can't be blank!"
end

Next, you'll modify the submit method to display the form anew should errors occur:

   def submit
      @question = Question.new(params[:question])
      if @question.save
         flash[:notice] = "Your question has been successfully sent
                           to customer service.
                           Would you like to submit another question?"
         render :action => "index"
      else
         render :action => "index"
      end
   end

Finally, to display the error messages should a field not be completed, add the following line to the top of the form view (/support/index.rhtml):

<%= error_messages_for("question") %>

Go ahead and submit the form, but this time not completing any of the fields, and you'll be greeted with a response as shown in the following screenshot, followed by presentation of the form anew:

Figure 2: The result generated by the form.

By following a similar process, you can take advantage of Rails' other validation methods to impose further constraints upon your user input! I'll introduce a few others below.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel