[英]SmarterCSV and resque results in undefined method close
我正在尝试使用Resque和SmarterCSV,但始终看到相同的错误:
undefined method 'close' for nil:NilClass
在我的日志中。 我不确定为什么会这样。 我进行了一些挖掘,发现它的人发现与文件位置错误有关,但是我只是将文件作为参数传递,它没有保存在任何地方。
我的表格:
<%= 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 %>
我的控制器:
def check_upload_file
Resque.enqueue(AddClientsFromScale, params[:file], params[:location])
redirect_to bulk_uploads_path
end
我的工人:
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
我的模特:
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
有人知道发生了什么吗?
问题是file
变量是一个散列,其中包含的数据不仅仅是文件本身。 线索是使用file["tempfile"]
打印的地方。 那就是您需要插入SmarterCSV的原因,因为那是引用您要处理的实际文件的方式。
SmarterCSV.process(file["tempfile"], options) do |row|
就我而言,我还有一个其他文件编码问题,我从SmarterCSV收到此错误:
警告:您正在尝试处理UTF-8输入,但是没有使用“ b:utf-8”选项打开输入。 请参见自述文件“关于文件编码的注释”。
最终这就是对我的帮助:
f = File.open(file["tempfile"], "r:bom|utf-8")
SmarterCSV.process(f, options) do |row|
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.