:failure and :success in form_remote_tag

I have a form that unfortunatly is not associated with a model as it
is created dynamically based on selections from a user. I have a
validation action called validate_the_form to make sure that fields
the user signifies as required are acctually required. On submit I
run the validation and redirect to the the form if there are any
errors displaying the errors on top. Really I want to mimic the way
that validate works in a model where there are errors on top as well
as highlighting the fields that are missing the info that is needed
but I also want to keep the values that are correct selected and
filled out.

I was taking a look at the options for form_remote_tag and figured
that I could submit the form via ajax and have set up the form remote
tag like this:

<%= form_remote_tag(:url => { :action => ‘validate_the_form’},
:update => { :success => “form_area”, :failure
=> “error_area” }) %>

my question is I am not sure where to go from here. i can’t figure
out how to define what is a failure. Am I going down the right track?
or is there another way to do this? I also looked in my copy of Agile
Web Dev and they have an example but it does not keep values that are
selected and or filled out in the form if there is a failure.

Thanks for all the help


On a side note I did look at the example on the wiki as well as this
one “Validations for non-ActiveRecord Model Objects” but I don’t
think that they will work for me as I don’t have a model for this
form. The form is built dynamically based on several other models but
because the options that I need to validate are not constant (for
example a set of radio buttons could require that there is a choice
or not require one at all) based on what the user defines. Since
these fields are completely unknown I am not sure where to go.


Here is a very nice way of still being able to use validations
without the need for a database model. It just fakes rails out into
thinking the model is an AR model and you just cant .save it , you
call .valid? on it to check the validations:

class Contact < ActiveRecord::Base
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new
(name.to_s, default, sql_type.to_s, null)

column :name, :string
column :city, :string
column :state, :string
column :phone, :string
column :email_address, :string

validates_presence_of :name, :email_address


-Ezra Z.
Yakima Herald-Republic Newspaper
[email protected]

On Dec 8, 2005, at 6:24 PM, Ezra Z. wrote:

columns << ActiveRecord::ConnectionAdapters::Column.new 


Oh and I forgot to credit technoweenie for writing it :wink:

-Ezra Z.
Yakima Herald-Republic Newspaper
[email protected]

I looked at that but not sure that it will work in my situation. as I
don’t know what will be required or not ahead of time. Since I don’t
know what will be required I can’t define the validate_presence_of
the fields. Also I don’t know what the field names are ahead of time
either. They are generated on the fly when the page is rendered. Here
is a look at my validate method that I have written. I think I am on
the right track writing my own action just not sure how to repopulate
the fields if there is an error.

def validate_the_form
@errors = []
@ theform = Form.find(params[: theformid])
# loop through all the fields to validate the responses given match
the requirements defined
# by the form author
@form.questions.each do |@question|
# Check to see if the question is required
if @question.required == 1
# we need to do something special to validate if the checkbox
type is required
# here we check to see if it is a checkbox
if @question.field_type == ‘checkbox’
# if it is a check box we loop thru the choices to check if they
are checked
counter = 0
@question.choices.each do |@choice|
if params["#{@choice.id}question_#{@question.id}"] != nil
counter = counter + 1
# if no check boxes are checked we kick out an error
if counter == 0
@errors << “#{@question.question_text}”
# if it is not a checkbox we make sure that there is not a null
if params[“question_#{@question.id}”] == nil or params
[“question_#{@question.id}”] == ‘’
@errors << “#{@question.question_text}”
# we next check our checkbox type to make sure that the minimum and
# maximum values defined by the offer are adhered to
if @question.field_type == ‘checkbox’
# make sure that maximum responses are adhered to
if @question.max_responses != nil
if checked > @question.max_responses
@errors << “#{@question.question_text} to many responses”
# make sure that minimum responses are adhered to
if @question.min_responses != nil
if checked < @question.min_responses
@errors << “#{@question.question_text} to few responses”
# if we have no errors submit the form other wise we kick back to
the form
if @errors.size == 0
render :action => ‘take’, :id => @theform.id, :errors => @errors

my view right now has allot of logic in it to build the form. I think
that I should be using helpers for it but it works right now. This
app is in scaffold stage right now. I just want to get it working and
than will go back and do some code cleanup during the the time that I
make the views pretty.

here is just a piece of the view:

<% if params[:errors] != nil %> <% 0.upto(params[:errors].size - 1) do |x| %> The Question: <%= params[:errors][x] %>
Is Required

<% end %> <% end %>

<%= flash[:notice] %>

<%= @theform.name %>
<%= form_remote_tag(:url => { :action => 'validate_the_form'}, :update => { :success => "form_area", :failure => "error_area" }) %> <%= hidden_field_tag(: theformid, @ theform.id) %> <% @questions.each do |@question| %> <%= @question.question_text %>
<% if @question.field_type == 'radio' %> <% @question.choices.each do |@choice| %> <% questionid = "question_#{@question.id}" %> <%= radio_button_tag("question_#{@question.id}", @choice.choice_text) %><%= h(@choice.choice_text) %>
<% end %>

I than go through a bunch of more elsifs to create the different
fields (checkbox, textfield, textarea and so on)

<%= submit_tag ‘Submit’ %>
<%= end_form_tag %>

As I said I think that I am heading in the right direction I am just
not sure how to kick back a failure from the validate method to the


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs