# Ajax and ruby problem validation model and error displays

**URL:** <https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072>\
**Category:** Rails\
**Created:** [November 21, 2005, 4:48pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072 "2005-11-21T16:48:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 21, 2005, 4:48pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072/1 "2005-11-21T16:48:13Z")

</div>

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](http://article.gmane.org/gmane.comp.lang.ruby.rails/30827)

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 21, 2005, 5:32pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072/2 "2005-11-21T17:32:45Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 21, 2005, 5:52pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072/3 "2005-11-21T17:52:58Z")

</div>

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.  
[http://farechase.yahoo.com](http://farechase.yahoo.com)

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 21, 2005, 8:10pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072/4 "2005-11-21T20:10:06Z")

</div>

hey, thanks

but i have a question,

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

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 21, 2005, 9:51pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072/5 "2005-11-21T21:51:05Z")

</div>

I have it in application\_helper.rb

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 22, 2005, 9:51am UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072/6 "2005-11-22T09:51:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 22, 2005, 10:31am UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072/7 "2005-11-22T10:31:32Z")

</div>

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 😛

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???

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 22, 2005, 1:16pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072/8 "2005-11-22T13:16:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 22, 2005, 11:23am UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-validation-model-and-error-displays/49072/9 "2005-11-22T11:23:53Z")

</div>

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
