Forum: Ruby on Rails How can I know if a file has been attached?

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.
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-23 04:08
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!
D93ae163042e0a937941e0ba4d8ed5d7?d=identicon&s=25 Eric (Guest)
on 2009-02-23 04:58
(Received via mailing list)
> 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
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2009-02-23 15:58
(Received via mailing list)
On Feb 22, 9:08 pm, Guillaume Loader <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
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-23 18:20
Perfect it works! Thank you!
This topic is locked and can not be replied to.