【发布时间】:2017-01-19 11:30:16
【问题描述】:
我正在尝试使用带有 resque 的后台作业导入 CSV 文件。它似乎运行但没有任何反应。当我不使用 resque 时,导入工作正常(我正在使用 resque,因为一些导入很大,我想转移到后台作业,以测试它的小 2 行 csv)
任何帮助都非常感谢,非常感谢! (我也是初学者,所以请忽略任何帮助:))
inventories_controller.rb:
def import
Resque.enqueue(Inventorycsvimport, params[:file], current_user.id)
redirect_to root_url, notice: "Inventory import job started."
end
工人工作清单csvimport.rb:
class Inventorycsvimport
@queue = :Inventorycsvimport
def self.perform()
Inventory.destroy_all(user_id: current_user.id)
Inventory.import(params[:file], current_user.id)
end
end
导入类inventory.rb:
class Inventory < ApplicationRecord
belongs_to :user
def self.import(file, user_id)
allowed_attributes = [ "user_id", "id","description","part_number","price","created_at","updated_at", "alternate_part_number", "condition_code", "qty", "mfg_code", "serial_number", "part_comments"]
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
inventory = find_by_id(row["id"]) || new
inventory.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k }
inventory.user_id = user_id
inventory.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::CSV.new(file.path)
when ".xls" then Excel.new(file.path, nil, :ignore)
when ".xlsx" then Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
end
这是我在 JOB 中遇到的错误:
【问题讨论】:
-
你有没有尝试过一步步调试,并尝试过实际失败的地方。请提供您的调查结果
-
你如何在工作中获得
current_user?好像没有作为参数传入。可能是工作中的nil。在这种情况下,您的验证失败,因为inventory需要有效的user,在这种情况下它不会被保存并且什么也不会发生(除非您应该在日志中看到它,因为它会产生ValidationError因为save!跨度> -
@Kkulikovskis 我将控制器更改为:def import Resque.enqueue(Inventorycsvimport, params[:file], current_user.id) redirect_to root_url,注意:“库存导入作业开始。”结束,现在我得到 Exception NameError Error undefined local variable or method `current_user' for Inventorycsvimport:Class
-
@Kkulikovskis 我用代码和工作错误更新了我的原始帖子 - 感谢您的帮助!!!!!!
标签: ruby-on-rails ruby csv redis resque