How show validation errors in form_remote_tag?

I suspect this has been asked 400 times, but Google yields 2-3 rather
complex gymnastics in blogs (and possibly dated), and searching list
archives on ruby-forum proves kinda useless.

Using standard forms, it’s fairly easy to use
@object.errors[:field_name] in the form to embed an error message right
next to the field that has the invalid entry. The controller uses a
simple conditional with redirect_to to re-display the form if needed.

I don’t understand how to reproduce this same straight-forward process
when the form uses a form_remote_tag.

Is there a simple, universal process for this, or does it really take 40
lbs of hacks to accomplish like the blogs all seem to suggest?

If it makes any difference, I do not want some generic red box at the
top of the page saying there’s errors below. I want the error messages
displayed with the input fields.

Quite lost – clues appreciated.

– gw

On Aug 16, 5:25 am, Greg W. [email protected]
wrote:

when the form uses a form_remote_tag.

Is there a simple, universal process for this, or does it really take 40
lbs of hacks to accomplish like the blogs all seem to suggest?

It should be reasonably straightforward to re-render the entire form.
It might get more complicated if you want to be cleverer that that. I
suppose you could have a div/span with a predictable id next to each
field then loop over object.errors, inserting relevant content into
each of these div/spans

Fred

Frederick C. wrote:

On Aug 16, 5:25�am, Greg W. [email protected]
wrote:

when the form uses a form_remote_tag.

Is there a simple, universal process for this, or does it really take 40
lbs of hacks to accomplish like the blogs all seem to suggest?

It should be reasonably straightforward to re-render the entire form.
It might get more complicated if you want to be cleverer that that. I
suppose you could have a div/span with a predictable id next to each
field then loop over object.errors, inserting relevant content into
each of these div/spans

So far I have something like this in the controller. Similar to what I’d
do for a non-Ajax routine.

if @object.custom_save
respond_to do |request_format|
request_format.js
end
else
render :action => ‘edit_method’
end

The view rendered by edit_method uses something like this:

<% if @object.errors.on(:field_name) -%> ERROR <% end -%>
<input type=“text” …etc… />

The RJS is pretty basic like this:

page.replace_html ‘formID’, :partial => the_path, :locals => {:object =>
@object}

The page starts with a div for viewing data only. Click and Edit button,
and it is replaced with an editable form which includes a Save button.

By the time the controller gets to render :action, it seems like the rjs
is ineffective. AFAICT (visually) there’s no actual updating of the HTML
via RJS.

– gw

Greg W. wrote:

I don’t understand how to reproduce this same [validation display] process
when the form uses a form_remote_tag.

So far I have something like this…

BTW – while I’ve been using Ruby and Rails for about 2 years now (on
top of using other languages for almost 10 years), much of that has been
straight Ruby stuff, and the essentials for non-Ajax Rails apps. I’m
understanding quite a bit of Rails now, but JavaScript and Ajax are
areas I’ve never spent much time studying. So I’m probably missing some
understanding about the processes and interplays about the XHR thing
that makes me think this should be simpler than it is.

– gw

Greg W. wrote:

Greg W. wrote:

I don’t understand how to reproduce this same [validation display] process
when the form uses a form_remote_tag.

Figured it out. I pretty much had it right, but I needed some vars
re-defined before the form was displayed the second time. So pulled that
out into a method which both the first & second pass actions could make
use of.

– gw

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs