Try this:
if @product && @product.price.blank?
# Do something here
end
In the code below, if @product is nil, the first condition is false so
the second condition is evaluated. @product.price will fail when
@product is nil.
Chris
On Jul 18, 8:38 am, Josh
<jjkie...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I had a piece of code in an rjs file that checked to see if a variable
> existed:
>
> if @product
> ... do something here ...
> end
>
> that worked fine, but then I also need to check and see if a column is
> empty or nil, so i changed it to this:
>
> if @product || !...@product.price.blank?
> ... do something here ...
> end
>
> this always gives me "You have a nil object when you didn''t
expect
> it!" errors
>
> is there a better way of doing this without nesting if statements
> inside eachother?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---