【问题标题】:SmarterCSV and resque results in undefined method closeSmarterCSV 和 resque 导致未定义的方法关闭
【发布时间】:2018-08-25 23:32:11
【问题描述】:

我正在尝试使用ResqueSmarterCSV,但一直看到同样的错误:

undefined method 'close' for nil:NilClass

在我的 resque 日志中。我不确定为什么会这样。我已经进行了一些挖掘,看到这一点的人发现这与文件位置错误有关,但我只是将文件作为参数传递,它没有保存在任何地方。

我的表单:

<%= 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

有人知道怎么回事吗?

【问题讨论】:

    标签: ruby-on-rails csv resque smartercsv


    【解决方案1】:

    问题在于file 变量是一个包含比文件本身更多的数据的散列。线索是您使用file["tempfile"] 打印它的位置。这就是您需要插入 SmarterCSV 的内容,因为它引用了您尝试处理的实际文件。

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

    就我而言,我遇到了一个额外的文件编码问题,我从 SmarterCSV 收到了这个错误:

    警告:您正在尝试处理 UTF-8 输入,但没有使用“b:utf-8”选项打开输入。请参阅 README 文件“关于文件编码的注意事项”。

    这就是最终为我所做的:

    f = File.open(file["tempfile"], "r:bom|utf-8")
    SmarterCSV.process(f, options) do |row|
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-31
      • 2012-05-21
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多