在Ruby中通过Tempfile写入文件
发布时间:2020-12-17 01:50:17  所属栏目:百科  来源:网络整理 
            导读:我有以下后台工作写入csv文件并通过电子邮件发送出去.我正在使用Tempfile类,因此在将其通过电子邮件发送给用户后,该文件将被删除.目前,当我查看csv文件时,我生成的结果如下所示: ["Client Application" "Final Price" "Tax" "Credit" "Base Price" "Billed
                
                
                
            | 
                         
 我有以下后台工作写入csv文件并通过电子邮件发送出去.我正在使用Tempfile类,因此在将其通过电子邮件发送给用户后,该文件将被删除.目前,当我查看csv文件时,我生成的结果如下所示: 
  
  
  
["Client Application" "Final Price" "Tax" "Credit" "Base Price" "Billed At" "Order Guid" "Method of Payment Guid" "Method of Payment Type"] ["web" nil nil nil nil nil nil "k32k313k1j3" "credit card"] 请忽略这些数据,但问题是,它是以ruby格式直接写入文件而不是删除“”和[]字符. 请参阅以下代码: class ReportJob
@queue = :report_job
 def self.perform(client_app_id,current_user_id)
  user = User.find(current_user_id)
  client_application = Application.find(client_app_id)
  transactions = client_application.transactions
  file = Tempfile.open(["#{Rails.root}/tmp/",".csv"]) do |csv|
    begin
     csv << ["Application","Price","Tax","Credit","Base Price","Billed At","Order ID","Payment ID","Payment Type"]
     transactions.each do |transaction|
      csv << "n"
      csv << [application.name,transaction.price,transaction.tax,transaction.credit,transaction.base_price,transaction.billed_at,transaction.order_id,transaction.payment_id,transaction.payment_type]
    end
   ensure
    ReportMailer.send_rev_report(user.email,csv).deliver
    csv.close(unlink_now=false)
    end
  end
end
end 
 这是使用tempfile类而不是csv类的问题吗?或者我可以做些什么来改变它写入文件的方式? 添加用于在邮件程序中读取csv文件的代码.我目前收到的TypeError表示“无法将CSV转换为字符串”. class ReportMailer < ActionMailer::Base
 default :from => "test@gmail.com"
  def send_rev_report(email,file)
     attachments['report.csv'] = File.read("#{::Rails.root.join('tmp',file)}")
      mail(:to => email,:subject => "Attached is your report")
    end
  end
end
解决方法
 问题是你实际上并没有将csv数据写入文件.您正在向文件句柄发送数组.我相信你需要这样的东西: 
  
  
  
        Tempfile.open(....) do |fh|
    csv = CSV.new(fh,...)
    <rest of your code>
end 
 正确设置CSV输出过滤. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
