femtowin femtowin
2005-Jul-16 10:56 UTC
Anything like (lineitem.product.title)?if_exists in freemarker?
hello all, do rails have Anything like (lineitem.product.title)?if_exists in freemarker? in freemarker if lineitem.product is null (lineitem.product.title)?if_exists would produce nothing, but in rails <%= lineitem.product.title %> would report error "undefined method `title'' for nil:NilClass" so how can I handle this situation when lineitem.product is sometimes null? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko Laine
2005-Jul-16 11:06 UTC
Re: Anything like (lineitem.product.title)?if_exists in freemarker?
On 16.7.2005, at 13:56, femtowin femtowin wrote:> hello all, > do rails have Anything like (lineitem.product.title)?if_exists in > freemarker? > in freemarker if lineitem.product is null > (lineitem.product.title)?if_exists would produce nothing, > but in rails <%= lineitem.product.title %> would report > error > "undefined method `title'' for nil:NilClass" > so how can I handle this situation when lineitem.product is > sometimes null?respond_to? [1] is probably what you''re after. http://raildock.mytechsupport.com/ri/respond_to? //jarkko> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Lord Khaos
2005-Jul-16 11:23 UTC
Re: Anything like (lineitem.product.title)?if_exists in freemarker?
On Sat, 2005-07-16 at 18:56 +0800, femtowin femtowin wrote:> hello all, > do rails have Anything like (lineitem.product.title)?if_exists in > freemarker?I think this will work. It came up on this list just a very few days ago. Might be worthy of a Wiki entry when I''m running on more sleep. @title =(lineitem.product.title rescue nil) unless @title ==nil # do what you want with title jarkko, wouldn''t respond_to? simply confirm if title is an attribute or no, regardless of it being nil, or not? Howard
femtowin femtowin
2005-Jul-16 11:48 UTC
Re: Anything like (lineitem.product.title)?if_exists in freemarker?
<%=lineitem.product.title rescue nil %>would do and generate nothing when product is nil. thanks. @title =(lineitem.product.title rescue nil) unless @title ==nil # do what you want with title I guess you mean writing code in controller, but my situation is in rhtml and I''m in a loop iterating lineitems, so set @title wouldn''t be a valid choice. On 7/16/05, Lord Khaos <lordkhaos-Wuw85uim5zDR7s880joybQ@public.gmane.org> wrote:> On Sat, 2005-07-16 at 18:56 +0800, femtowin femtowin wrote: > > hello all, > > do rails have Anything like (lineitem.product.title)?if_exists in > > freemarker? > I think this will work. It came up on this list just a very few days > ago. Might be worthy of a Wiki entry when I''m running on more sleep. > > @title =(lineitem.product.title rescue nil) > unless @title ==nil # do what you want with title > > jarkko, wouldn''t respond_to? simply confirm if title is an attribute or > no, regardless of it being nil, or not? > > Howard > > > >
Jarkko Laine
2005-Jul-16 19:49 UTC
Re: Anything like (lineitem.product.title)?if_exists in freemarker?
On 16.7.2005, at 14:23, Lord Khaos wrote:> > jarkko, wouldn''t respond_to? simply confirm if title is an attribute or > no, regardless of it being nil, or not?Oh yes, sorry, I was too hasty to just read the NoMethodError. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails