How can I know if a file has been attached?


#1

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!


#2

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

Answer should be somewhere here:
http://www.tutorialspoint.com/ruby/ruby_methods.htm


#3

On Feb 22, 9:08 pm, Guillaume L. <rails-mailing-l…@andreas-
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
switchingtorails.com


#4

Perfect it works! Thank you!