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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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
with
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 !
==
Dylan
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 =>
'form'
That way you still get highlighting for the failed fields, and it makes
it
easy to provide a fallback if javascript is disabled.

joshua
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
checked
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
would
avoid using them.
Check out
http://railsexpress.de/blog/articles/2005/11/18/co...
You want those ajax calls to be snappy!

joshua
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)
  page.show 'flash_box'
else
  page.hide 'flash_box'
end

I got the hint from liquid's comment to
http://rails.techno-weenie.net/question/2006/2/23/...

Ed
This topic is locked and can not be replied to.