Forum: Ruby on Rails file_column - how to test for empty column?

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.
jeremy*pinnix (Guest)
on 2006-01-13 19:56
(Received via mailing list)
I am using the excellent file_column, but have run into an issue.

I am wanting the user to be able to *not* upload an image if they so
choose. Then I am trying to determine if that column is empty or not,
and display a generic image if that column is empty.

I have tried both of the following code blocks:

<% if  @newsentry.image -%>
<%= link_to(image_tag((url_for_image_column "newsentry", "image",
:small), :size => "50x50", :border => 0, :class =>"news_photo"),
:controller => 'news', :action => 'article', :id => @newsentry) %>
<% else -%>
<%= link_to(image_tag("/images/news/photo_news_sm.jpg", :size =>
"50x50", :border => 0, :class =>"news_photo"), :controller =>
'@newsentry', :action => 'article', :id => @newsentry) %>
<% end -%>

and

<% if (! @newsentry.image.nil?) and (@newsentry.image.length > 0) -%>
<%= link_to(image_tag((url_for_image_column "newsentry", "image",
:small), :size => "50x50", :border => 0, :class =>"news_photo"),
:controller => 'news', :action => 'article', :id => @newsentry) %>
<% else -%>
<%= link_to(image_tag("/images/news/photo_news_sm.jpg", :size =>
"50x50", :border => 0, :class =>"news_photo"), :controller =>
'@newsentry', :action => 'article', :id => @newsentry) %>
<% end -%>

Either way I get the error:

"cannot convert nil into String"

and it is failing here:
./script/../config/../vendor/plugins/file_column/lib/magick_file_column.rb:47:in
`exists?'

which tells me that it thinks the if statement is true, but then when
file_column tries to test its existence that it fails...

I looked in the db, and that field is indeed  <null> on the ones that
didn't have files uploaded.

What is the proper way to test this?

BTW, when I remove entries that don't have an image uploaded,  it
works just fine...

Thanks,
jeremy
Roberto S. (Guest)
on 2006-01-13 20:33
(Received via mailing list)
file_column has a method which tells you whether the file has just been
uploaded or not, take a look at he source code, which has all the
documentation in it.
This topic is locked and can not be replied to.