Forum: Ruby on Rails remote_form_for ajax validation

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
34edfb526d15bc8204ad0393efabe536?d=identicon&s=25 Isaac (Guest)
on 2009-03-16 02:14
(Received via mailing list)

I have been reading a lot about this, but I do not manage to do a nice
validation. I already managed to save data to the database, to
validate a field, to display a "Everything went well" message. I can
also display an error message, but the view does not end up properly.

This is the form:

<% remote_form_for(product) do |f| %>
    <%= f.error_messages %>
      <%= f.label :title  %><br />
      <%= f.text_field :title %>
      <%= f.label :body  %><br />
      <%= f.text_area :body %>
            <%= f.submit "Update" %> รณ
  <% end %>

This is the controller:

def update
    @product = Product.find(params[:id])
    respond_to do |format|
      if @product.update_attributes(params[:product])
        flash[:notice] = 'OK!'
        format.html { redirect_to(@product) }
      else[:notice] = 'Error!!'
        format.html { render :action => "show" }
        format.xml { render :xml => @product.errors,  :status
=> :unprocessable_entity }

The model:

class Product < ActiveRecord::Base
  validates_presence_of :title

And the RJS template:

page.replace_html :description, :partial => "product", :object =>
page.replace_html :notice, flash[:notice]
page.visual_effect (:fade, "notice", :duration => 2)

But I thing there is a better way to validate forms.

Does any one have an example about validating, and displaying
customized messages.


24bf6fba90a71630f27c0ae0e08646f5?d=identicon&s=25 Eric Surname (bobzimuta)
on 2009-03-25 22:46
(Received via mailing list)
Like commonly with regular forms, I prefer javascript validation
before the form submits. I happened across what I think is a valid

<% remote_form_for(product, :before => "if(!validateForm(this))
{ return false; }") %>

Where validateForm is a javascript function that does the form
validation. Note that using this method, the javascript function MUST
return a boolean, either true for valid data, or false if not. If it's
not valid then the form won't submit. Within the javascript you can
put alerts or change the html with error messages so that the user can
correct their input data.
This topic is locked and can not be replied to.