Active Admin Exporting to CSV file
The code below is working:
collection_action :import_csv, method: :get do
users = User.all.order("id ASC")
csv = CSV.generate( encoding: 'Windows-1251' ) do |csv|
csv << [ "Id", "Name", "Gender", "Email", "Mobile number","Email Verified",
"Mobile Verified", "Language Preference", "Counselling Preference", "Job Role Name",
"Liked", "Disliked", "Favourited", "Shared", "Video Watched"]
users.each do |u|
email_verified = u.is_email_verified ? "Yes" : "No"
mobile_verified = u.is_mobile_verified ? "Yes" : "No"
counselling_preference = u.counselling_preference ? "Yes" : "No"
userarry = [ u.id, u.name, u.gender, u.email, u.mobile_number, email_verified,
mobile_verified, u.language_preference, counselling_preference]
csv << userarry + ["", "","","", "", ""]
end
end
send_data csv.encode('Windows-1251'), type: 'text/csv; charset=windows-1251; header=present', disposition: "attachment; filename=accounting_report.csv"
end
action_item :add do
link_to "Export to CSV", import_csv_admin_users_path, method: :get
end
Just add section inside admin
csv do
column :id
column :name
column :email
end
Check also active admin customize CSV
If you open link, you will see line
column(:author) { |post| post.author.full_name }
It adds authors name into the post csv file, his way you can add any field you want