views:

954

answers:

2

I would like to have Ajax form in Rails so i'm using form_remote_tag. The field i would like to submit is email address - how can i use the Rails validations together with form_remote_tag?

+1  A: 

In your form processing controller action decide whether

  1. you load flash[:notice] with validation errors and lead the user back to the form view
    OR
  2. let her go ahead and process the valid data, (leading to the next step)

You can (should) make this decision based on validations

def create
  m = Model.new(params)
  if m.valid?
    m.save
    #load flash with succes message
  else
    #load flash with error messages from m.errors
    #render the form again
  end
end
Fer
+3  A: 

We're using form_remote_tag with the :update option and paste the whole form into a partial. Once the form is submitted and validation fails, the partial is being rendered again and all error messages will show up.

This is the workflow:

  1. Call "edit" action
  2. Display form from a partial, use form_remote_tag with :update option
  3. Submit form to "update" action
  4. Validate data
  5. Validation failed? Display partial again with error messages
  6. Validation passed? Display form with some success message
Matt