Hi all I have the following code in my controller: if @comment.valid? captcha_url "http://captchator.com/captcha/check_answer/#{captcha_code}/#{@comment.captcha}" result = open(captcha_url) unless result.read == "1" @comment.errors.add(:captcha, "Captcha wurde nicht korrekt eingegeben") raise "#{@comment.valid?}" end end Although I''m adding an error to :captcha, when I raise @comment.valid? on the next line this still gives me true! Why is that? I just added an error to @comment, didn''t I?! Maybe it''s because I''m only using an attr_accessor for captcha? class Comment < ActiveRecord::Base validates_presence_of :captcha attr_accessor :captcha end Thanks Josh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well, it seems that obj.valid? and obj.save remove all manually added errors?! @comment.errors.add_to_base("Captcha wurde nicht korrekt eingegeben") raise "valid? #{@comment.valid?} - errors: #{@comment.errors.inspect}" results in "valid? false - errors: #<ActiveRecord::Errors:0x23d028c @base=#<Comment id: nil, commentable_id: 38313, user_id: 1, subject: "", body: "", created_at: nil, updated_at: nil, commentable_type: "Article">, @errors={"body"=>["can''t be blank"], "subject"=>["can''t be blank"]}>" and raise "errors: #{@comment.errors.inspect}" results in "errors: #<ActiveRecord::Errors:0x1878538 @base=#<Comment id: nil, commentable_id: 38313, user_id: 1, subject: "", body: "", created_at: nil, updated_at: nil, commentable_type: "Article">, @errors={"base"=>["Captcha wurde nicht korrekt eingegeben"]}>" Why the heck does ActiveRecord remove my custom errors when calling .valid??? Thanks a lot for help Josh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-20 10:10 UTC
Re: xxx.valid? still true after xxx.errors.add(...)?
On Apr 20, 10:26 am, Joshua Muheim <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Well, it seems that obj.valid? and obj.save remove all manually added > errors?! > > @comment.errors.add_to_base("Captcha wurde nicht korrekt > eingegeben") > raise "valid? #...@comment.valid?} - errors: > #...@comment.errors.inspect}" > > results in "valid? false - errors: #<ActiveRecord::Errors:0x23d028c > @base=#<Comment id: nil, commentable_id: 38313, user_id: 1, subject: "", > body: "", created_at: nil, updated_at: nil, commentable_type: > "Article">, @errors={"body"=>["can''t be blank"], "subject"=>["can''t be > blank"]}>" > > and > > raise "errors: #...@comment.errors.inspect}" > > results in "errors: #<ActiveRecord::Errors:0x1878538 @base=#<Comment id: > nil, commentable_id: 38313, user_id: 1, subject: "", body: "", > created_at: nil, updated_at: nil, commentable_type: "Article">, > @errors={"base"=>["Captcha wurde nicht korrekt eingegeben"]}>" > > Why the heck does ActiveRecord remove my custom errors when calling > .valid???Calling valid? recomputes the errors (there''s no difference at the end of the day between an error you added yourself and one from a ''normal'' validations. If you want to add errors yourself you should be doing so from a validation Fred> > Thanks a lot for help > Josh > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Apr 20, 10:26�am, Joshua Muheim <rails-mailing-l...@andreas-s.net> > wrote: >> body: "", created_at: nil, updated_at: nil, commentable_type: >> @errors={"base"=>["Captcha wurde nicht korrekt eingegeben"]}>" >> >> Why the heck does ActiveRecord remove my custom errors when calling >> .valid??? > > Calling valid? recomputes the errors (there''s no difference at the end > of the day between an error you added yourself and one from a ''normal'' > validations. If you want to add errors yourself you should be doing so > from a validation > > FredOK, Thanks for the explanation :-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---