Kyle Heon
2005-Nov-12 00:43 UTC
Handling a nil value when using FileColumn''s url_for_file_column
Being a Ruby newbie I don''t know how to properly check for a nil value. In my view I have the following code (uses FileColumn): <%= image_tag( url_for_file_column("gallery", "gallery_thumbnail", "thumb") ) %> Problem is that for sometimes there isn''t a gallery_thumbnail and when that happens it blows up with the following error: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.first I''ve tried the following tests with no luck: <%= image_tag( url_for_file_column("gallery", "gallery_thumbnail", "thumb") unless @gallery.gallery_thumbnail.nil? ) %> <%= image_tag( url_for_file_column("gallery", "gallery_thumbnail", "thumb") unless @gallery.gallery_thumbnail == nil ) %> <%= url_for_file_column( "gallery", "gallery_thumbnail", "thumb" ) if !@gallery.gallery_thumbnail.nil? %> How should I be testing for this? What is the proper Ruby way for handling nil values? Kyle Heon kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org
Ezra Zygmuntowicz
2005-Nov-12 01:18 UTC
Re: Handling a nil value when using FileColumn''s url_for_file_column
On Nov 11, 2005, at 4:43 PM, Kyle Heon wrote:> Being a Ruby newbie I don''t know how to properly check for a nil > value. > > In my view I have the following code (uses FileColumn): > > <%= image_tag( url_for_file_column("gallery", "gallery_thumbnail", > "thumb") > ) %> > > Problem is that for sometimes there isn''t a gallery_thumbnail and > when that > happens it blows up with the following error: > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.first > > I''ve tried the following tests with no luck: > > <%= image_tag( url_for_file_column("gallery", "gallery_thumbnail", > "thumb") > unless @gallery.gallery_thumbnail.nil? ) %> > > <%= image_tag( url_for_file_column("gallery", "gallery_thumbnail", > "thumb") > unless @gallery.gallery_thumbnail == nil ) %> > > <%= url_for_file_column( "gallery", "gallery_thumbnail", "thumb" ) if > !@gallery.gallery_thumbnail.nil? %> > > How should I be testing for this? What is the proper Ruby way for > handling > nil values? > > Kyle Heon > kheon-Wuw85uim5zDR7s880joybQ@public.gmane.orgKyle- This idiom works good for me when using the url_for_file_column and other things that will blow up if there is a nil value. I would rather it just do nothing when there is a nil. So here is how I handle it:> <%= image_tag( url_for_file_column("gallery", "gallery_thumbnail", > "thumb")) rescue nil %>HTH- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Kyle Heon
2005-Nov-12 01:35 UTC
RE: Handling a nil value when using FileColumn''s url_for_file_column
Hmm, that''s odd. I am pretty sure I tried that as well and it didn''t work. Who knows, it''s working now. Now, one more question. What would be the "Rails" way of defaulting to a "no image" image in this same situation? Sorry for all the questions. Thanks for the tip! Kyle Heon kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org Kyle- This idiom works good for me when using the url_for_file_column and other things that will blow up if there is a nil value. I would rather it just do nothing when there is a nil. So here is how I handle it:> <%= image_tag( url_for_file_column("gallery", "gallery_thumbnail", > "thumb")) rescue nil %>HTH- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org