0

Using Rails 4,i want to apply import and export functionality,when i click on EXPORT TO TEXT link all my table data which is present in table is downloaded to text. Same functionality i have applied for excel but now i want it for text.So i don't have any idea on how may i do it and what i may write in controller.

This is my previous code which i have followed from railcast.

def import_machine_attendance
  @emp = Employee.all
      respond_to do |format|
      format.html
      format.csv { send_data @emp.to_csv }
      format.xls
  end
end

<%= link_to "CSV", import_machine_attendance_machine_attendances_path(format: "csv") %> |
Hardik Upadhyay
  • 2,639
  • 1
  • 19
  • 34
Mahesh Sharma
  • 221
  • 1
  • 13

1 Answers1

0

Try this with override to_csv,

def self.to_csv
    attributes = %w{id email name}

    CSV.generate(:col_sep => "#") do |csv|
      csv << attributes

      all.each do |employee|
        csv << attributes.map{ |attr| employee.send(attr) }
      end
    end
  end
Hardik Upadhyay
  • 2,639
  • 1
  • 19
  • 34
  • in my model it is def self.to_csv(options = {}),may i replace this with CSV.generate(:col_sep => "#") do |csv| – Mahesh Sharma Mar 22 '17 at 11:00
  • thnaks it worked. will you also answer my question if you know the solution which i previously posted...???? following is the link.... http://stackoverflow.com/questions/42920570/preview-documents-in-iframe-in-rails http://stackoverflow.com/questions/42873678/how-may-i-apply-sleep-functionality-on-array-in-ruby-on-rails – Mahesh Sharma Mar 22 '17 at 11:11
  • this data is coming from a single table.what if i want the data to be accessed from multiple table. for i want the department name of any employee then how may i write?????????? – Mahesh Sharma Mar 22 '17 at 13:01
  • this is in the format attributes = %w{id email name} but when i write attributes = %w{id email id.department.name} i am getting the error.So how may i access the data from other table and write it in attributes. – Mahesh Sharma Mar 22 '17 at 13:02
  • how's its possible to get the data from datatables,via references......so is there any way i may get data from multiple table using the above method as you described....but in that i also want to add some dropdowns....like month and year.... month = params[:pf_detail][:month] year = params[:pf_detail][:year] like this....so i may get the data for this month and year............. – Mahesh Sharma Mar 23 '17 at 04:37
  • it is not necessary to set attributes as i describe in example you can made your own logic. – Hardik Upadhyay Mar 23 '17 at 05:08
  • do you know the solution of this?????????????????????? http://stackoverflow.com/questions/42358767/sending-pdf-via-email-in-rails – Mahesh Sharma Mar 23 '17 at 09:04
  • i want to send pdf to every employee which i select in a checkbox. – Mahesh Sharma Mar 23 '17 at 09:06
  • http://stackoverflow.com/questions/43025173/issue-in-rake-dbload-command-while-switching-to-mysql-database @hardik pls reply if you know the solution............. – Mahesh Sharma Mar 26 '17 at 04:59
  • this is the link of my new question............http://stackoverflow.com/questions/43038387/how-may-i-copy-all-my-existing-application-data-from-sqlite3-to-mysql-in-rails – Mahesh Sharma Mar 27 '17 at 05:24
  • its very urgent bro.....all my application has stopped working............. – Mahesh Sharma Mar 27 '17 at 05:27