尝试使用延迟作业上传文件
def import
@report = current_user.reports.create!(name: params[:report_name])
@contact = @report.contacts.delay.import(params[:file], params[:info_type])
flash[:success]= "Contacts importing, check back in 2-3 minutes"
redirect_to contacts_url
end
返回:
undefined method `import' for []
当我尝试没有关联时:
def import
@report = current_user.reports.create!(name: params[:report_name])
Contact.delay.import(params[:file], params[:info_type], @report.id)
flash[:success]= "Contacts importing, check back in 2-3 minutes"
redirect_to contacts_url
end
我明白了:
undefined method `name' for nil:NilClass
导入:
def self.import(file, info, report_id)
CSV.foreach(file.path) do |row|
input = row*""
# input = hashy["input"]
case info
when "email"
begin
contact_hash = FullContact.person(email: input)
if contact_hash.status = 200
Contact.create!(input: input, contact_hash: contact_hash.to_json, info_type: info, found: true, pending: false, report_id: report_id)
elsif contact_hash.status = 202
Contact.create!(input: input, contact_hash: contact_hash.to_json , info_type: info, found: true, pending: true, report_id: report_id)
end
rescue FullContact::NotFound
Contact.create!(input: input, contact_hash: nil, info_type: info, found: false, report_id: report_id)
rescue FullContact::Invalid
end
when "telephone"
begin
Contact.create!(input: input, contact_hash: FullContact.person(phone: input).to_json, info_type: info, report_id: report)
rescue FullContact::NotFound
Contact.create!(input: input, contact_hash: "Not Found", info_type: info, report_id: report)
rescue FullContact::Invalid
end
end
end
end
我也不应该使用gmaps4rails gem,它将列纬度,经度和gmaps默认为nil,直到联系人保存到数据库中。这是为什么?
答案 0 :(得分:0)
使用self.import
。它将起作用而不仅仅是import
def self.import
end