Handling a nil value when using FileColumn's url_for_file_co


#1

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 H.
removed_email_address@domain.invalid


#2

On Nov 11, 2005, at 4:43 PM, Kyle H. wrote:

when that
unless @gallery.gallery_thumbnail.nil? ) %>
nil values?

Kyle H.
removed_email_address@domain.invalid

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 Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#3

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 H.
removed_email_address@domain.invalid

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 Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid