# Nil Issue

**URL:** <https://www.ruby-forum.com/t/nil-issue/91733>\
**Category:** Rails\
**Created:** [April 11, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/nil-issue/91733 "2007-04-11T22:54:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [April 11, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/nil-issue/91733/1 "2007-04-11T22:54:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [April 11, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/nil-issue/91733/2 "2007-04-11T23:07:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [April 12, 2007, 1:36am UTC](https://www.ruby-forum.com/t/nil-issue/91733/3 "2007-04-12T01:36:00Z")

</div>

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  
book,  
[http://www.pragmaticprogrammer.com/titles/fr\_ltp/](http://www.pragmaticprogrammer.com/titles/fr_ltp/)

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

> **[Programming Ruby (2nd edition)](https://pragprog.com/titles/ruby/index.html)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

Good luck!

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [April 11, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/nil-issue/91733/4 "2007-04-11T23:31:51Z")

</div>

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.  
thanks,

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [April 12, 2007, 4:12am UTC](https://www.ruby-forum.com/t/nil-issue/91733/5 "2007-04-12T04:12:10Z")

</div>

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.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [April 12, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/nil-issue/91733/6 "2007-04-12T17:11:31Z")

</div>

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.
