Mahesh Sharma
Mahesh Sharma

Reputation: 221

Sending Pdf via email in Rails

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.

Upvotes: 2

Views: 1003

Answers (1)

Hardik Upadhyay
Hardik Upadhyay

Reputation: 2659

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

Upvotes: 1

Related Questions