I am trying to use Resque and SmarterCSV but keep seeing the same error:
undefined method 'close' for nil:NilClass
in my resque logs. I'm not sure why it's happening. I've done some digging and people who have seen this found it has to do with a file location being wrong, but I'm simply passing the file as a param, it's not saved anywhere.
My Form:
<%= form_tag check_upload_file_path, multipart: true do %>
<%= file_field_tag :file %>
<%= select_tag 'location', options_from_collection_for_select(Location.real, 'id', 'name'), include_blank: true %>
<br>
<%= submit_tag "Preview", class: "btn btn-awaken btn-sm approve-me", name: 'preview' %>
<% end %>
My Controller:
def check_upload_file
Resque.enqueue(AddClientsFromScale, params[:file], params[:location])
redirect_to bulk_uploads_path
end
My worker:
class AddClientsFromScale
@queue = :validate_file
puts "adding clients from scale..."
def self.perform(file, location_id)
p file, location_id
WeighIn.check_file(file, location_id)
end
end
My model:
class WeighIn < ActiveRecord::Base
@hash_for_new_clients = {
' ID' => 'scale_id',
'Full Name' => 'name',
}
def self.check_file(file, location_id)
options = {:key_mapping => @hash_for_new_clients, :strings_as_keys => true, :keep_original_headers => true, :remove_unmapped_keys => true}
# prints the file and contents properly
p "file: ", file["tempfile"]
SmarterCSV.process(file, options) do |row|
p row
end
end
end
Anyone know what's going on?
The issue is that the file
variable is a hash containing more data than just the file itself. The clue is where you print it using file["tempfile"]
. That's what you need to plug into SmarterCSV, because that is what references the actual file you're trying to process.
SmarterCSV.process(file["tempfile"], options) do |row|
In my case I had an additional file encoding issue where I received this error from SmarterCSV:
WARNING: you are trying to process UTF-8 input, but did not open the input with "b:utf-8" option. See README file "NOTES about File Encodings".
This was what did it for me in the end:
f = File.open(file["tempfile"], "r:bom|utf-8")
SmarterCSV.process(f, options) do |row|
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.