# Handling a nil value when using FileColumn's url\_for\_file\_co

**URL:** <https://www.ruby-forum.com/t/handling-a-nil-value-when-using-filecolumns-url-for-file-co/47693>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/handling-a-nil-value-when-using-filecolumns-url-for-file-co/47693 "2005-11-12T12:50:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kheon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@kheon](https://www.ruby-forum.com/u/kheon)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/handling-a-nil-value-when-using-filecolumns-url-for-file-co/47693/1 "2005-11-12T12:50:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kheon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@kheon](https://www.ruby-forum.com/u/kheon)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/handling-a-nil-value-when-using-filecolumns-url-for-file-co/47693/2 "2005-11-12T12:50:11Z")

</div>

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

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![kheon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@kheon](https://www.ruby-forum.com/u/kheon)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/handling-a-nil-value-when-using-filecolumns-url-for-file-co/47693/3 "2005-11-12T12:50:11Z")

</div>

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

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid
