Checking if file_field is empty?

Hi, I am writing some simple upload functionality for an application. I can't figure out how to check if file@field is empty. Currently I am trying to use an if statement, so the post action will only occure if 'upload' is not nil. I have also tried using the .blank method with no success.

Any advice would be great. Thanks in advance, Jen.

If statement: #posts to the data model. if !:upload.nil?      post = Upload.save(params[:upload]) puts "file uploaded" render 'upload' repo.commit_all('params[:message]') else redirect_to 'upload'    end

Hi, I am writing some simple upload functionality for an application. I can't figure out how to check if file@field is empty. Currently I am trying to use an if statement, so the post action will only occure if 'upload' is not nil. I have also tried using the .blank method with no success.

Any advice would be great. Thanks in advance, Jen.

If statement: #posts to the data model. if !:upload.nil?

Are you trying to check whether param[:upload] has a value? Is so then you can use if !params[:upload].blank?

blank? returns true for nil or an empty string. nil? returns true for nil but false for empty string.

Colin

It would be better to delegate that job to Upload model.

Then you could write something like this:

#posts to the data model.

post = Upload.new(params[:upload])

if post.save

puts “file uploaded”

render ‘upload’

repo.commit_all(params[:message])

else

redirect_to ‘upload’

end

Hi, I have been experimenting with the code from the replies to this post so far, but still can’t achieve what I want. If a file is selected it is uploaded just fine. However if no file is selected the application spits out the following error: NoMethodError in UploadController#create

undefined method `original_filename' for nil:NilClass

Rails.root: /home/resource_portal/website

Application Trace | Framework Trace | Full Trace

rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
app/models/upload.rb:13:in `save'
app/controllers/upload_controller.rb:13:in `create'
rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/abstract_controller/base.rb:151:in `process_action'
rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/rendering.rb:11:in `process_action'

...

I'm guessing I therefore need to be able to validate whether there

is something in ‘file_field’ before the post action happens, but I’m not sure how to do this. Any other suggestions would be great!

Also Now I'm confused about the best place for this code to go. I

was originally just putting one if statement in my controller, but as this is validation I guess it should go in the model.

I should probably also point out that files are not being stored in

a database. Instead I am using GIT to version them and keep track of their history.

My current code is attached.

Thanks,

Jen.

upload_controller.rb (476 Bytes)

upload.rb (642 Bytes)

upload.rhtml (326 Bytes)

Hi, I have been experimenting with the code from the replies to this post so far, but still can't achieve what I want. If a file is selected it is uploaded just fine. However if no file is selected the application spits out the following error: NoMethodError in UploadController#create

undefined method `original_filename' for nil:NilClass

Rails.root: /home/resource_portal/website

Application Trace | Framework Trace | Full Trace

rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing' app/models/upload.rb:13:in `save' app/controllers/upload_controller.rb:13:in `create'

You said that you have taken note of the previous replies, but looking at the code in upload_controller.rb you have not tested params[:upload] before calling Upload.save in the create action, which was the point of your original question I think. As I previously suggested you need to use something like if !params[:upload].blank? to prevent save being called when params[:upload] is nil or empty. What else you need to do in that action when it is empty I do not know as I do not know the details of what you want to achieve.

Colin

Hi Colin, I did try implementing your suggestion in a previous experiment, which game me the same result. If I click the 'save upload' button and have not selected a file to upload the application falls over with the same error as in my previous post. I need some way of checking whether file_field is blank, and if it is prompting the user to select a file.

I am not sure where in the process of attempting to submit a file this check needs to happen, though I assume it is before the post occurs.

If this explanation is not clear enough please let me know and I will attempt to explain it better.

I am not sure if the fact that I am not using a database impacts on this. As stated in my previous post the files are uploaded to a directory, then committed to a git repo.

Thanks in advance for any further help, Jen.

Note the code below does work, as long as a file is selected for upload. Controller code: def create repo = Grit::Repo.new("/home/resource_portal/website/public/data/upload") if !params[:upload].blank? #posts to the data model.      post = Upload.save(params[:upload]), (params[:message]) puts "file uploaded" repo.commit_all(:message) render 'upload' else flash.now[:error] = "Please select a file to upload" redirect_to 'upload'    end end

Hi Colin, I did try implementing your suggestion in a previous experiment, which game me the same result. If I click the 'save upload' button and have not selected a file to upload the application falls over with the same error as in my previous post. I need some way of checking whether file_field is blank, and if it is prompting the user to select a file.

It could not possibly give you exactly the same error on the same line of code, as the test would have prevented that line from being executed. It may have been a similar error on a different line of code. As I said before you can test for the field blank or nil using params[:upload].blank?

Colin

As Colin said, you shouldn’t be getting the exact same error at the exact same place. Would be good to check it out.

Based on the code you posted, you are not using ActiveRecord features in Upload model, so do away with it and use ActiveModel instead.

Here’s a great Railscast on this - #219 Active Model - RailsCasts

I have modified the code a little bit to use ActiveModel instead of ActiveRecord. This should work fine.

Here it is - https://gist.github.com/1039543

There are still some issue with the code with respect to following standards. You should rename the index action to be ‘new’, because that’s what it is doing. Similarly, change the view file from upload.rhtml to new.html.erb

Name of the controller should be plural, so it should be uploads controller.

I am also assuming that you have a route in place in your routes.rb for upload, if not, it should have something like this:

resource :upload

Thanks for the tip on active models! I don't know much about the different types but I think this may be the answer to my problems. Though I have not been very exact with giving exact line nums etc for my errors I do understand if statements and what I am trying to do.

Thanks to everyone else for help and suggestions so far. Hopefully I'll solve it tomorrow!

Thanks, Jen.