How to display for errors using js request

html rendering is done this way : ( sorry… haml code…)

= form_for(resource, :as => resource_name, :url =>
user_registration_path(I18n.locale) ) do |f|
= render “shared/error_messages”, :target => [resource]

and the shared/error_messages partial is quite standard ( made
visible or not… if no errors…)

#resource_error_messages{:style => “display:#{errorsInForms(target) >
0 ? ‘visible’ : ‘none’}”}
#error_explanation
- target.each do |field|
- if field.errors.any?
%ul
- field.errors.full_messages.each do |msg|
%li= msg.html_safe unless msg.nil?

this runs very well … but I need to output it with a js request
( render :action => ‘create.js’ )
in my create.js.erb , I wrote :

$(’#resource_error_explanation’).html("<%= escape_javascript( render
‘shared/error_messages’, :target => [resource] ) -%>");

to render the errors, then I show the div
$(’#resource_error_messages’).show();

but the errors are NOT rendered at all… what should I write
instead ??

thanks for your feedback

You forgot to post you controller code, that the most important part

I finally solved this issue …passing the locals …

$(’#resource_error_explanation’).html("<%= escape_javascript( render
‘shared/error_messages’, :target => [resource] ) -%>");
is not valid …

in the controller I have now @target = [resource] and I am
passing :locals => {:target => @target}
I modified also the partial to use target and that’s it…

it tooks time to switch my mind to non-obstrusive js , but I
understand it better and of course I appreciate it

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