views:

44

answers:

2

Hey guys,

I'm using http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000086 to validate records.

My form is currently a remote form, using RJS. My question, is how to I return the :message (for errors) to the page through ajax (and I assume the create.rjs file)?

Best, Elliot

A: 

The Rails docs don't make it easy to find, but the answer is here: http://api.rubyonrails.org/classes/ActiveRecord/Errors.html

You can access any errors that come up through your model's object, and then in your RJS you format and return the messages. An example with a Post model might look something like this:

page.visual_effect :highlight, :message
unless @post.errors.empty?
  @post.errors.each_error do |attr, err|
    page.insert_html :bottom, :message, %(#{attr} #{err.message}<br>)
  end
else
  page.replace_html :message, "Huzzah! I posted!<br>"
end

where :message is the id of the DIV you're using to display the errors.

nyousefi
A: 

hi Elliot,

there is a cool plugin called live validations, which will allow you do your model validations with AJAX

have a look at this link

http://github.com/porras/livevalidation.git

cheers, sameera

sameera207