Nil Issue

I am using acts_as_attachment to upload multiple files and it works
great. My problem is when I go to display the files and images. If one
or more files are missing I get an error stating “can’t convert nil
into String” what I then tried is the following:

<% if “”.blank? %>

<% else %>
<%= link_to image_tag('/images/common/reader_icon.jpg'), @report.public_filename %>
<% end %>

This clears the error but then it also hides the pdf icon for all
other records. How would I go about hiding the pdf icon if there isn’t
a file associated with that record but show it if there is a file?

Hope this makes sense and thanks for any help.

Chad wrote:

<% end %>

What are you trying to do here…

<% if “”.blank? %>

That will always evaluate to ‘true’. I suspect what you really want is
something more like…

<% if @report.missing? %>

and implement you’re own ‘missing?’ method to test if the file is
available or not.

That’s not really Rails as much as Ruby. In your if statement, you’re
condition is checking if an empty string is blank, which it will always
be. You’d get the exact same result if it said <% if 1 == 1 %>, or even
<% if true %>. The condition you need to be checking is whether or not
the file in question is available on the server.

If you’re new to programming, I’d highly recommend starting with this

or if you’ve programmed in other languages before, but just aren’t
grasping the OO Syntax of Ruby, then this would be better.

Good luck!

I am fairly new to Rails and am not 100% sure if I understand what you
mean. Could you please explain further or provide and example.

So if I understand you what I would need to do is something along the
lines of this?

<% if file_is_in_database %>

<%= link_to image_tag('/images/common/reader_icon.jpg'), @report.public_filename %>
<% else %>
<% end %>

If this is what I need to do, then how do I go about verifying that
the file is in the database?

I am currently reading the Pickaxe book trying to grasp Ruby.

Yup, that’s right. I’m not familiar with acts_as_attachment, but I
would bet it’s got a method to check for the existance of said file.