我正在努力解决这个错误。我有一个像这样的控制器动作:
def import
InventoryItem.import_list(params[:file], current_vendor.id)
redirect_to vendors_dashboard_path, notice: "Inventory Imported"
end
我的班级方法:
class << self
def import_list(file, vendor_id)
vendor = Vendor.find(vendor_id)
SmarterCSV.process(file.path,
{:key_mapping => { vendor.import_preference.brand_symbol => :brand,
vendor.import_preference.product_symbol => :product,
vendor.import_preference.price_symbol => :price,
vendor.import_preference.upc_symbol => :upc}}) do |row|
params = row.first.merge(:store_id => vendor.stores.first.id)
inventory_item = InventoryItem.create(params)
end
end
handle_asynchronously :import_list
end
当我尝试执行时,我得到未定义的方法错误。此方法可以在不调用handle_asynchronously
的情况下工作,当我将其设为普通的self.import
类方法时。我在这里做错了什么?
*更新**
错误的服务器日志:
Started POST "/inventory_items/import" for 127.0.0.1 at 2014-10-06 09:30:53 -0700
Processing by InventoryItemsController#import as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "file"=>#<ActionDispatch::Http::UploadedFile:0x000001081d3ab8 @original_filename="Inventory1 - Sheet1.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"Inventory1 - Sheet1.csv\"\r\nContent-Type: text/csv\r\n", @tempfile=#<Tempfile:/var/folders/6Q/6Qj+aFh4A4orYbGW-nE+++TI/-Tmp-/RackMultipart201406-65416-1dcpxp9>>, "commit"=>"Import"}
Vendor Load (0.9ms) SELECT "vendors".* FROM "vendors" WHERE "vendors"."id" = 25 LIMIT 1
Completed 500 Internal Server Error in 99ms
NoMethodError (undefined method `name' for nil:NilClass):
app/controllers/inventory_items_controller.rb:6:in `import'
相关堆栈跟踪:
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:50:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:480:in `block in dump_ivars'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:478:in `each'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:478:in `dump_ivars'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:150:in `visit_Object'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:344:in `block in visit_Array'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:344:in `each'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:344:in `visit_Array'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:467:in `block in emit_coder'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:465:in `each'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:465:in `emit_coder'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:450:in `dump_coder'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:126:in `accept'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:92:in `push'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych.rb:244:in `dump'
delayed_job (4.0.4) lib/delayed/backend/base.rb:81:in `payload_object='
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.13) lib/active_record/base.rb:498:in `initialize'
delayed_job (4.0.4) lib/delayed/backend/base.rb:32:in `new'
delayed_job (4.0.4) lib/delayed/backend/base.rb:32:in `enqueue'
delayed_job (4.0.4) lib/delayed/message_sending.rb:12:in `method_missing'
delayed_job (4.0.4) lib/delayed/message_sending.rb:46:in `block in handle_asynchronously'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
答案 0 :(得分:8)
params[:file]
将无法正确编组。而不是将params[:file]
对象作为第一个参数传递,而是传递文件路径:
def import
InventoryItem.import_list(params[:file].path, current_vendor.id)
redirect_to vendors_dashboard_path, notice: "Inventory Imported"
end
然后直接在worker方法中使用路径:
class << self
def import_list(path, vendor_id)
vendor = Vendor.find(vendor_id)
SmarterCSV.process(path, {:key_mapping => { vendor.import_preference.brand_symbol => :brand, vendor.import_preference.product_symbol => :product, vendor.import_preference.price_symbol => :price, vendor.import_preference.upc_symbol => :upc}}) do |row|
params = row.first.merge(:store_id => vendor.stores.first.id)
inventory_item = InventoryItem.create(params)
end
end
handle_asynchronously :import_list
end