Forum: Ruby on Rails Handling a nil value when using FileColumn's url_for_file_co

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B6efa20772f1404cdd68840e95722891?d=identicon&s=25 kheon (Guest)
on 2005-11-12 12:50
(Received via mailing list)
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@comcast.net
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 ezra (Guest)
on 2005-11-12 12:50
(Received via mailing list)
On Nov 11, 2005, at 4:43 PM, Kyle Heon wrote:

> when that
> unless @gallery.gallery_thumbnail.nil? ) %>
> nil values?
>
> Kyle Heon
> kheon@comcast.net


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@yakima-herald.com
B6efa20772f1404cdd68840e95722891?d=identicon&s=25 kheon (Guest)
on 2005-11-12 12:50
(Received via mailing list)
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@comcast.net

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@yakima-herald.com
This topic is locked and can not be replied to.