Sending Pdf via email in Rails

别说谁变了你拦得住时间么 提交于 2019-12-22 14:05:47

问题


emp_ids = params[:employee_ids]

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.


回答1:


Try This,

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

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

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

Hope it will help you



来源:https://stackoverflow.com/questions/42358767/sending-pdf-via-email-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!