简体   繁体   English

SmarterCSV和resque导致未定义的方法关闭

[英]SmarterCSV and resque results in undefined method close

I am trying to use Resque and SmarterCSV but keep seeing the same error: 我正在尝试使用ResqueSmarterCSV,始终看到相同的错误:

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. 问题是file变量是一个散列,其中包含的数据不仅仅是文件本身。 The clue is where you print it using file["tempfile"] . 线索是使用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的原因,因为那是引用您要处理的实际文件的方式。

SmarterCSV.process(file["tempfile"], options) do |row|

In my case I had an additional file encoding issue where I received this error from SmarterCSV: 就我而言,我还有一个其他文件编码问题,我从SmarterCSV收到此错误:

WARNING: you are trying to process UTF-8 input, but did not open the input with "b:utf-8" option. 警告:您正在尝试处理UTF-8输入,但是没有使用“ b:utf-8”选项打开输入。 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|
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM