Forum: Ruby on Rails RJS and Form Validation (Best Practice)

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.
Dylan S. (Guest)
on 2006-03-23 20:37
(Received via mailing list)
Hello All !

I've been using RJS extensively, but haven't done any form validation
it until now.

Should I be manually passing the RJS template some models error messages
from the controller.... or
... should I be using error_messages_for in order to do the display ?

I've implemented the first approach, but it seems like quite a hack.
Any insight would be greatly appreciated !
Thanks !
Joshua B. (Guest)
on 2006-03-23 22:53
(Received via mailing list)
I do it the same as with non-ajax forms, just throw your <%=
error_messages_for :model %> at the top of a form partial
If there's an error creating, or updating the model, just
page.replace_html'id_of_el_surrounding_form_partial', :partial =>
That way you still get highlighting for the failed fields, and it makes
easy to provide a fallback if javascript is disabled.

Dylan S. (Guest)
on 2006-03-24 20:09
(Received via mailing list)
Thanks Joshua, this works perfectly.
In my case, the :partial is actually a component... and... haven't
if RJS supports :component.... hmmm...  I'm lazy :)
Joshua B. (Guest)
on 2006-03-24 21:23
(Received via mailing list)
I have a feeling RJS won't support components, and even if it did I
avoid using them.
Check out
You want those ajax calls to be snappy!

Ed Howland (Guest)
on 2006-05-24 00:05
(Received via mailing list)
Hi Dylan,

I added a <div id="flash_box" style="display: none">&nbsp;</div> just
ahead of my form_remote_to tag helper.

Then in the action method of the form (create.rjs in my case:

if @client.errors.length > 0
  page.replace_html 'flash_box', error_messages_for(:client) 'flash_box'
  page.hide 'flash_box'

I got the hint from liquid's comment to

This topic is locked and can not be replied to.