How do I use form_remote_tag along with validations?

I would like to have an Ajax form in Rails, so I am using form_remote_tag. The field I would like to submit an email address - how can I use Rails validations along with form_remote_tag?

0


a source to share


2 answers


We use form_remote_tag with the: update option and insert the entire form into a partial. After the form is submitted and validated, no partial processing is performed and all error messages will be displayed.

This is the workflow:



  • Call action "change"
  • Display form from partial, use form_remote_tag with: update option
  • Submit the form to "update".
  • Confirm data
  • Failed to complete validation? Display the partial number again with the error message
  • Checkout passed? Display form with some success message
+3


a source


In your form controller processing routine, decide

  • you load flash [: notice] with a validation error and bring the user back to the view form
    OR
  • let it go ahead and process the valid data, (leading to the next step)


You can (should) make this decision based on validation

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

      

+1


a source







All Articles