[英]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.