# How can I know if a file has been attached?

**URL:** <https://www.ruby-forum.com/t/how-can-i-know-if-a-file-has-been-attached/161062>\
**Category:** Rails\
**Created:** [February 23, 2009, 4:08am UTC](https://www.ruby-forum.com/t/how-can-i-know-if-a-file-has-been-attached/161062 "2009-02-23T04:08:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 23, 2009, 4:08am UTC](https://www.ruby-forum.com/t/how-can-i-know-if-a-file-has-been-attached/161062/1 "2009-02-23T04:08:24Z")

</div>

Hello everyone!

I have a form where we can attach files to a post.  
But I want to do different things depending wether a file is attached or  
not.

So my question is : it it possible to know if a file is attached? I  
would say yes, we just need to see if the parameter “file” is empty.

Here are my parameters :  
{  
“commit”=\>“Create”,  
“post”=\>{“title”=\>“Test”,  
“subject”=\>“desc test”,  
“post\_attachment”=\>  
{  
“post\_id”=\>"",  
“file”=\>"", # \<----------- This is the line with the file  
“creator\_user\_id”=\>""  
}  
}

How can I test if the variable “file” is empty or not?  
Thank you!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 23, 2009, 4:58am UTC](https://www.ruby-forum.com/t/how-can-i-know-if-a-file-has-been-attached/161062/2 "2009-02-23T04:58:59Z")

</div>

> How can I test if the variable “file” is empty or not?  
> Thank you!

Answer should be somewhere here:

> **[Ruby - Methods](http://www.tutorialspoint.com/ruby/ruby_methods.htm)**
>
> Ruby Methods - Ruby methods are very similar to functions in any other programming language. Ruby methods are used to bundle one or more repeatable statements into a single unit.

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 23, 2009, 3:58pm UTC](https://www.ruby-forum.com/t/how-can-i-know-if-a-file-has-been-attached/161062/3 "2009-02-23T15:58:56Z")

</div>

On Feb 22, 9:08 pm, Guillaume L. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> {  
> }
> 
> How can I test if the variable “file” is empty or not?

Assuming Rails 2.2 or higher:

if params[:post\_attachment][:file].present?

# do file upload here

end

If Rails 2.1 or earlier,

unless params[:post\_attachment][:file].blank?

# do file upload here

end

Does this help?

Jeff

[purpleworkshops.com](http://purpleworkshops.com)  
[switchingtorails.com](http://switchingtorails.com)

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 23, 2009, 6:20pm UTC](https://www.ruby-forum.com/t/how-can-i-know-if-a-file-has-been-attached/161062/4 "2009-02-23T18:20:43Z")

</div>

Perfect it works! Thank you!
