Ajax and ruby problem validation model and error displays


#1

hey,

in my model i have this

validates_presence_of :first_name, :last_name, :email

my popup is like 300x250, but if i have a lot of validates then the
popup could
be to small. and when i submit empty textfields, my popup get stuck :s:s

now how can i implement this error displays to the user in my ajax???
my other messages about my ajax (also the code)
http://article.gmane.org/gmane.comp.lang.ruby.rails/30827


#2

You might want to parse the validation information. I have done
something
like:

def parse_errors(obj, strip=false)
return nil if obj.nil?
err = error_messages_for(obj)
err[/(.)

There were problems with the following
fields:</p>(.

)</div>/]
if !err.nil?
return “” if $2.nil?
str = “” + $2.to_s + “
str.gsub!(/

    /,’’);
    str.gsub!(/</ul>/,’’);
    str.gsub!(/’/, “\\’”) unless strip==true
    return str
    end

    and you can do whatever you want with the resulting string.
    What I do is to render a partial that would extract the validation
    errors,
    and using javascript it would set the content of a div that displays
    them.
    Something like:
    res.rhtml

    do_some_other_stuff()


#3

I was trying to set up scaffolding, but when I go to
edit/new to add entries into my database. I get this
error

No rhtml, rxml, or delegate template found for
admin/_form

1:

Editing article


2:
3: <%= start_form_tag :action => ‘update’, :id =>
@article %>
4: <%= render_partial ‘form’ %>
5: <%= submit_tag ‘Edit’ %>
6: <%= end_form_tag %>

Has anyone seen this error before? If I comment out
the “<%= render_partial ‘form’ %>” line it goes to the
edit action but nothing is displayed.


Yahoo! FareChase: Search multiple travel sites in one click.


#4

hey, thanks

but i have a question,

def parse_errors(obj, strip=false)
where does this reffer to??, how must i implement it?


#5

I have it in application_helper.rb


#6

hey,

cant get it to work, …

can u put more code or so, so i know that i should do??
i got this

Methods added to this helper will be available to all templates in the

application.
module ApplicationHelper

def parse_errors(obj, strip=false)
return nil if obj.nil?
err = error_messages_for(obj)
err[/(.)

There were problems with the following
fields:</p>(.

)</div>/] if !err.nil?
return “” if $2.nil?
str = “” + $2.to_s + “
str.gsub!(/

    /,’’);
    str.gsub!(/</ul>/,’’);
    # str.gsub!(/’/, “\\’”) unless strip==true
    return str
    end

    end

    then i use this <%= parse_errors(‘user’) %>
    in my edit.rhtml, or do i this wrong, please help, cuz i need this
    validation,
    or ajax is no good for me.


#7

i made it work, but i need something else ;):wink:

this is my process til now;

-a list of users (with show and edit link)
-click on edit link, a popup with the user info
-user edits values, clicks button, buttons disables , and loading
message
-after update a message is shown (succesfull or error) in a popup

and now :stuck_out_tongue:

if the update is succesfull i want a refresh of the list, is this
possible to
have 2 update div at the same time??

this is the edit popup (edit.rhtml):
div_user_error is the second popup with the confirmation message, and
here i
want also a refresh of ‘userlist’

<%= form_remote_tag ( :url => { :action => :update, :id => @user },
:update => “div_user_error” ,
:loading => ‘item_loading()’,
:complete => “new Effect.Highlight(‘user#{@user.id}’);
user_updated();changeSty(‘info’,‘noshow_link’);” ) %>

the confirmation message (update.rhtml)
<% if @error %>
<%= parse_errors(‘user’) %>
<% else %>
User succesfully updated.
<% end %>

is it possible like to a ajax update even when the user doesnt click on
something, like on load of body or something???


#8

You can update 2 divs but you will have to update the second one
yourself.
How you do it it’s up to you.
on :complete you are specifying a javascript function that receives the
content.
:complete=>‘function(result){$(“second”).innerHTML =
result.responseText}’

Look in the generated HTML code and you will see that the Ajax request
is
simply a javascript function.


#9

Both Rails and Ajax are good for you, you just need to study them
more…
The purpose of that parse_errors method is to format the validation
errors,
if you have any.
In your case you would get them after an update or create, so you should
place that <%=parse errors(‘user’) unless @user.nil?%> in whatever
.rhtml is
rendered at the end of your update request. So wherever you had before
<%=error_messages_for ‘user’%> put that parse_errors call