Empty file_column

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.


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

<% @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.

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

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