# Empty file\_column

**URL:** <https://www.ruby-forum.com/t/empty-file-column/113084>\
**Category:** Rails\
**Created:** [August 27, 2007, 12:58pm UTC](https://www.ruby-forum.com/t/empty-file-column/113084 "2007-08-27T12:58:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolbox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f8ce5/32.png) [@coolbox](https://www.ruby-forum.com/u/coolbox)\
**Post date:** [August 27, 2007, 12:58pm UTC](https://www.ruby-forum.com/t/empty-file-column/113084/1 "2007-08-27T12:58:51Z")

</div>

Hey guys,

I am trying to create a helper to distinguish wether a field that  
employs the use of file\_colum is empty or not for the purpose of a  
users personal image.

The reason that i am doing this is because when displaying the records  
i am presented with the following error:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.PHOTO\_relative\_path

…when calling the PHOTO field with this code:

\<%= image\_tag url\_for\_file\_column(“user”, “PHOTO”) %\>

This is because some users do not have an uploaded photo.

What i want to do is to work out whether or not the PHOTO field is  
empty or not and act accordingly. If the field has a url to an image  
then display that image using the image\_tag else if the field is empty  
i want to either insert the url of a default image or just force the  
system to automatically display the default image.

Based on the way that file\_colum works im going to guess it is easier  
to just point the system to an image location if the field is empty  
instead of trying to force a url into the db record.

Ok i am a bit of a newby at this so can someone confirm to me that  
this would indeed be achieved with a helper or would this code go  
somewhere else?

I have been toying with the following:

\<% if collector.PHOTO.blank? %\>  
\<%= image\_tag("/images/avatar\_thumb.jpg") %\>  
\<% else %\>  
\<%= image\_tag("#{collector.PHOTO}") %\>  
\<% end %\>

…which works interms of determining if the field is blank or not but  
it does not display the image if the field is not empty. In order to  
do that i would have to use the call i had been previously using:

\<%= image\_tag url\_for\_file\_column(“user”, “PHOTO”) %\>

…but this then throws up the initial error again. Plus i know that  
this is in the List view at current and im sure it would be tidier to  
do it somewhere else.

Cheers in advance for any help.

Pete

---

<div class="post-metadata">

**Author:** ![coolbox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f8ce5/32.png) [@coolbox](https://www.ruby-forum.com/u/coolbox)\
**Post date:** [August 28, 2007, 12:35pm UTC](https://www.ruby-forum.com/t/empty-file-column/113084/2 "2007-08-28T12:35:26Z")

</div>

first of all, if it isn’t too late, i wouldn’t use file\_column, as the  
development of that is stopped, and there are other capable better  
plugins out there that do uploading just as well.

that’s for starters. but, if it’s too late / not important enough to  
switch to something else, i’d have to raise two pointers:

in order for an image to be displayed here  
\<%= image\_tag url\_for\_file\_column(“user”, “PHOTO”) %\>

you will need the following:

- a GLOBAl variable (with the ‘@’ sign), which means, in this case, u’ll  
need

\<% @user = User.find(id) %\>  
\<%= image\_tag url\_for\_file\_column(“user”, “PHOTO”) %\>

now, make sure u don’t error out:  
\<%= image\_tag url\_for\_file\_column(“user”, “PHOTO”) unless  
@user.PHOTO.nil? %\>

(basically, in order to escape the errors raised, u need two checks -  
one to see the user exists/defined, and one to see the PHOTO (btw, why  
is this caps?) field isn’t nil either.

---

<div class="post-metadata">

**Author:** ![coolbox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f8ce5/32.png) [@coolbox](https://www.ruby-forum.com/u/coolbox)\
**Post date:** [August 28, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/empty-file-column/113084/3 "2007-08-28T16:53:33Z")

</div>

actually because of ActiveRecord magic, you could just do @user.photo?  
this  
will check for both nil values and empty strings.

Also check out UploadColumn, it works pretty much the  
same  
way file\_column does, but it’s under active development, and less  
buggy/confusing.\</shameless plug\>

/Jonas
