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.


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>