Forum: Ruby on Rails Ajax and ruby problem validation model and error displays

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.
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 brutyn_nick (Guest)
on 2005-11-21 16:48
(Received via mailing list)
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
531eb73f8fbf05a197721d02b4e6aadb?d=identicon&s=25 bogdan.ionescu (Guest)
on 2005-11-21 17:32
(Received via mailing list)
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[/(.*)<p>There were problems with the following
fields:<\/p>(.*)<\/div>/]
if !err.nil?
return "" if $2.nil?
str = "<b>" + $2.to_s + "</b>"
str.gsub!(/<ul>/,'');
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

<script>
$('result').innerHTML = <%=parse_errors('user')%>
</script>
do_some_other_stuff()
93aea4ff5331ee2c926cebd8df6be579?d=identicon&s=25 Jm Mc (railsnewb)
on 2005-11-21 17:52
(Received via mailing list)
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: <h1>Editing article</h1>
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.
http://farechase.yahoo.com
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 brutyn_nick (Guest)
on 2005-11-21 20:10
(Received via mailing list)
hey, thanks

but i have a question,

def parse_errors(obj, strip=false)
where does this reffer to??, how must i implement it?
531eb73f8fbf05a197721d02b4e6aadb?d=identicon&s=25 bogdan.ionescu (Guest)
on 2005-11-21 21:51
(Received via mailing list)
I have it in application_helper.rb
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 brutyn_nick (Guest)
on 2005-11-22 09:51
(Received via mailing list)
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[/(.*)<p>There were problems with the following
fields:<\/p>(.*)<\/div>/] if !err.nil?
      return "" if $2.nil?
      str = "<b>" + $2.to_s + "</b>"
      str.gsub!(/<ul>/,'');
      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.
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 brutyn_nick (Guest)
on 2005-11-22 10:31
(Received via mailing list)
i made it work, but i need something else ;);)

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 :p

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???
531eb73f8fbf05a197721d02b4e6aadb?d=identicon&s=25 bogdan.ionescu (Guest)
on 2005-11-22 11:23
(Received via mailing list)
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
531eb73f8fbf05a197721d02b4e6aadb?d=identicon&s=25 bogdan.ionescu (Guest)
on 2005-11-22 13:16
(Received via mailing list)
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.

<body onload="myfunc">
<script>
function myfunc(){
form=$('my_form')
new Ajax.Updater('div_to_update', '/Controller/Action/',
{asynchronous:true,
evalScripts:true,onComplete:function(request){},
parameters:Form.serialize
(form)});
}
</script>
</body>
This topic is locked and can not be replied to.