I know this is super basic, but I need some help. In my view, which displays a form, if a field is nil or if there are errors when the form is submitted, display "A", otherwise, display "B". I can''t figure out how to test for condition 1 OR 2. Instead the code always executes "A". Sample code looks like this: <% if model.field.nil? or model.errors %> <%= A %> <% else %> <%= B %> <% end %> Any thoughts? Thanks!!! -- 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Mon, 28 Jul 2008, Becca Girl wrote:> > I know this is super basic, but I need some help. > > In my view, which displays a form, if a field is nil or if there are > errors when the form is submitted, display "A", otherwise, display "B". > > I can''t figure out how to test for condition 1 OR 2. Instead the code > always executes "A". > > Sample code looks like this: > <% if model.field.nil? or model.errors %> > <%= A %> > <% else %> > <%= B %> > <% end %> > > Any thoughts?model.errors will return an Errors object, even if it''s empty. So it will always be true in the ''if'' clause context. You''d probably want to do model.errors.empty? David -- Rails training from David A. Black and Ruby Power and Light: * Advancing With Rails August 18-21 Edison, NJ * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David A. Black wrote:>> Any thoughts? > > model.errors will return an Errors object, even if it''s empty. So it > will always be true in the ''if'' clause context. You''d probably want to > do model.errors.empty? >Thank you so much! I didn''t realize that. Works like a charm now. -- 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 -~----------~----~----~----~------~----~------~--~---
On Jul 28, 2008, at 5:29 PM, Becca Girl wrote:> I know this is super basic, but I need some help. > > In my view, which displays a form, if a field is nil or if there are > errors when the form is submitted, display "A", otherwise, display > "B". > > I can''t figure out how to test for condition 1 OR 2. Instead the > code > always executes "A". > > Sample code looks like this: > <% if model.field.nil? or model.errors %>You''ll always get at least an empty array from model.errors <% if model.field.nil? || model.errors.size > 0> > <%= A %> > <% else %> > <%= B %> > <% end %> > > Any thoughts? > > Thanks!!!I also tend to use || rather than or in this type of expression so the ultra-low precedence of ''or'' doesn''t cause seemingly odd behavior at times. (Of course, I worked in C for many years so || just seems natural.) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Mon, 28 Jul 2008, Rob Biedenharn wrote:> > On Jul 28, 2008, at 5:29 PM, Becca Girl wrote: >> I know this is super basic, but I need some help. >> >> In my view, which displays a form, if a field is nil or if there are >> errors when the form is submitted, display "A", otherwise, display >> "B". >> >> I can''t figure out how to test for condition 1 OR 2. Instead the >> code >> always executes "A". >> >> Sample code looks like this: >> <% if model.field.nil? or model.errors %> > > You''ll always get at least an empty array from model.errorsStrictly speaking, it''s an ActiveRecord::Errors object, not an array. But it has strongly array-like inclinations :-) David -- Rails training from David A. Black and Ruby Power and Light: * Advancing With Rails August 18-21 Edison, NJ * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---