2

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.

Mahesh Sharma
  • 221
  • 1
  • 13
  • when you check than each checked employee get salary his/her salary slip in mail right? – Divyang Hirpara Feb 21 '17 at 09:20
  • yes...i want that in email of every employee which i selected using checkbox.I have also written in ActionMailer but sdon know may i render so many pdf in email. – Mahesh Sharma Feb 21 '17 at 10:01
  • do anyone knows how may i achieve this functionality??? i need it urgently.... – Mahesh Sharma Feb 22 '17 at 03:56
  • why don't you iterate through the array of employee_ids and send a mail for each? Since email content differs, you cannot just say ActionMailer to send one email to different employees with different content. If content would be the same, of course you could send that one email to all different employees at once. so why don't you just use something alike: params[:employee_ids].each {|employee_id| YourSalaryMailer.send_salary_pdf(employee_id).deliver_now } – bpieck Feb 24 '17 at 10:07
  • ok i'll try.....thanks for the reply.....!!!!!! – Mahesh Sharma Feb 27 '17 at 04:09
  • but how may i apply loops in it for sending email???? will you give an example....!!!!! – Mahesh Sharma Feb 27 '17 at 04:17
  • only want to the basic syntax of how may i iterate it. – Mahesh Sharma Feb 27 '17 at 09:11
  • if anyone knows the answer then pls reply...........!!!!!!!!!!! – Mahesh Sharma Mar 23 '17 at 09:34

1 Answers1

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

Hardik Upadhyay
  • 2,639
  • 1
  • 19
  • 34
  • to store generated pdf file, you can store anywhere you like – Hardik Upadhyay Mar 23 '17 at 11:15
  • [4834, 5152, 5151, 5151, 5159, 5162, 5151, 5151, 5138, 5003, 4843, 5112, 4609, 4629, 5166, 4904, 4815, 4899, 4738, 4675, 4724, 4699, 4699, 4608, 4969, 4898, 5125, 4856, 4922, 4880, 4880, 5079, 4673, 4889, 4889, 4780, 4902, 4691, 4939, 4791, 4631, 5025, 4647, 4922, 4906, 4952, 4911, 4882, 4732, 5127, 4787, 4733, 4915, 5151, 5151, 4839, 5021, 4937, 4809, 4840, 5097, 4776, 4855, 4864, 4864, 4735, 4716, 4921, 4985, 4977, 5125, 4701, 4783, 4744, 4792, 5166, 4963, 4904, 4978, 4899, 4759] – Mahesh Sharma Mar 23 '17 at 11:16
  • this is my array....i want to count duplicate elements in array and display how many time that element is repeated...means 5151 is repeated 2 time.so it must display 5151=2 – Mahesh Sharma Mar 23 '17 at 11:17
  • result = array.each_with_object(Hash.new(0)) { |key,counts| counts[key] += 1 } – Hardik Upadhyay Mar 23 '17 at 11:20
  • its in hash....how may i convert it into simple object so i may get....5151=6,4922=2 and so on............... – Mahesh Sharma Mar 23 '17 at 11:22
  • coz 5151 is employee id...so i want to dispay it like this....for eg 5151 name is abc...so it must come like this.........abc=2 and so on.......... – Mahesh Sharma Mar 23 '17 at 11:24
  • you can't get directly into simple form you have to iterate over it. – Hardik Upadhyay Mar 23 '17 at 11:45
  • @employee_leav_requests = EmployeeLeavRequest.where.not(first_reporter_id: nil).where(current_status: "Pending").pluck(:first_reporter_id) – Mahesh Sharma Mar 23 '17 at 11:50
  • if first reporter has 6 employees under it and EmployeeLeavRequest is the table name.....then first_reporter_id may be common for many employee...so i want a name of employees whose – Mahesh Sharma Mar 23 '17 at 11:51
  • reporting manager is same.for eg [5151,{4605,4606,4607,4608}] here 5151 is first_reporter and it has 6 employee under him. – Mahesh Sharma Mar 23 '17 at 11:58
  • i have posted one question...reply in that....http://stackoverflow.com/questions/43270910/date-format-error-in-ruby-in-rails-4-with-sql-server-as-a-database-on-windows-7 @hardik – Mahesh Sharma Apr 07 '17 at 10:03