Reputation: 29
I have a page where I upload a list of users to an action upload data. There on success/failure it lists me the list of invalid users. This is a one time action the lsit of users is not saved to database. I need to be able to write these user list to a file and allow the user to download this file. I do not have a clue on how to proceed. Any help would be appreciated.
Upvotes: 1
Views: 1368
Reputation: 3323
1. require the CSV library to controller
require 'csv'
2. Code in controller
def export_to_csv
#get all invalid users here and then modify according to yoou logic
@users = User.find(:all)
csv_string = CSV.generate do |csv|
csv << ["Id", "Name", "status","Role"]
@users.each do |user|
csv << [user.id, user.name, user.status, user.role]
end
end
send_data csv_string,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=users.csv"
end
Upvotes: 3