Brutyn nick
2005-Nov-21 15:39 UTC
Ajax and ruby problem validation model and error displays
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
Bogdan Ionescu
2005-Nov-21 16:28 UTC
Re: Ajax and ruby problem validation model and error displays
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()
On 11/21/05, Brutyn nick
<brutyn_nick-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org>
wrote:>
> 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
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
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
Brutyn Nick
2005-Nov-21 19:06 UTC
Re: Ajax and ruby problem validation model and error displays
hey, thanks but i have a question, def parse_errors(obj, strip=false) where does this reffer to??, how must i implement it?
Bogdan Ionescu
2005-Nov-21 20:20 UTC
Re: Re: Ajax and ruby problem validation model and error displays
I have it in application_helper.rb On 11/21/05, Brutyn Nick <brutyn_nick-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> > hey, thanks > > but i have a question, > > def parse_errors(obj, strip=false) > where does this reffer to??, how must i implement it? > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Brutyn nick
2005-Nov-22 08:43 UTC
Re: Ajax and ruby problem validation model and error displays
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.
Brutyn nick
2005-Nov-22 09:25 UTC
Re: Ajax and ruby problem validation model and error displays
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???
Bogdan Ionescu
2005-Nov-22 09:26 UTC
Re: Re: Ajax and ruby problem validation model and error displays
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
On 11/22/05, Brutyn nick
<brutyn_nick-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org>
wrote:>
> 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.
>
>
>
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Bogdan Ionescu
2005-Nov-22 12:15 UTC
Re: Re: Ajax and ruby problem validation model and error displays
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>
On 11/22/05, Brutyn nick
<brutyn_nick-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org>
wrote:>
> 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???
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails