我正在使用活动管理员在我的Rails 4.0应用程序中使用管理功能。我有一个列出
的用户表我也在列表顶部放了一个按钮。
action_item only: :index do
link_to 'Export to CSV', '#'
end
我想导出用户ID名称&单击此按钮时,将电子邮件发送到csv文件。请帮我这样做
答案 0 :(得分:5)
只需在admin
中添加部分即可csv do
column :id
column :name
column :email
end
同时检查active admin customize CSV
如果您打开链接,则会看到
行column(:author) { |post| post.author.full_name }
它将作者姓名添加到帖子csv文件中,他可以添加你想要的任何字段
答案 1 :(得分:5)
以下代码正常运作:
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