在Rails中通过电子邮件发送Pdf [英] Sending Pdf via email in Rails

查看:98
本文介绍了在Rails中通过电子邮件发送Pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre class = snippet-code-html lang-html prettyprint-override> emp_ids = params [:employee_ids]

我正在Rails上的一个项目中工作,我有一个Employee List页面,此列表是员工的薪水单的列表。在该列表中,我已经申请了复选框,并在底部有一个发送按钮。我希望如果我选择多个复选框,电子邮件应该发送给所有员工,包括薪水单pdf。如果我选​​择了复选框,那么我就可以做到这一点。所有的薪水单都是为我选择的员工而来的,但是在该pdf页面中,我无法应用电子邮件功能,因此我希望它能够直接发生。我使用了wicked pdf和actionmailer,但是我很困惑如何发送数组多个员工ID(例如,我已在索引页[employee_ids]中编写了此类代码,并使用params访问了它:

I am working on a project in rails where i have an Employee List page,This list is the List of Salaryslip of an employee.In that list i have applied checkboxes and at the bottom there is an Send button.I want that if i select multiple checkboxes,Email should be gone to all the employees including the pdf of salary slip.I am able to do this if i select checkbox the on clicking on submit all the salaryslip come for the employee who i have selected but in that pdf page i can't apply email functionality so i want it to be directly happen.I have used wicked pdf and actionmailer but i'm confused how may i send an array of multiple employees ids(like this i have written in my index page [employee_ids] and accessed it using params[:employee_ids] in controller) to Action Mailer for send email.

推荐答案

尝试一下,

def generate_slip
  employees = Employee.where(id: emp_ids)
  employees.each do |employee|
   @employee = employee
   pdf_name = employee.name + Date.today.to_s
   pdf = render_to_string pdf: "pdf.html.erb", template: "employees/pdf.html.erb", layout: 'layouts/pdf', encoding: "UTF-8"
   file_path = Rails.root.join('pdfs',"#{pdf_name}.pdf")
   File.open(save_path, 'wb') do |file|
     file << pdf
   end
   UserMailer.send_file(to: employee.email, subject: "Slip")
  end 
end

您需要在员工目录下创建一个名为 pdfs的文件夹,并在员工目录下创建一个名为 pdf.html.erb的文件。

You need to create one folder called 'pdfs' into public directory and one file called 'pdf.html.erb' under employees directory.

所有实例变量都可以在pdf.html.erb文件中使用。

All instance variable available into pdf.html.erb file.

希望它会为您提供帮助

这篇关于在Rails中通过电子邮件发送Pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆