尝试使用延迟作业上传文件
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,直到将联系人保存到数据库中。这是为什么?