active admin 1.0.0
rails 5
csvダウンロードをカスタマイズしてダウンロードをしようとしていますが。
画面で絞り込んだ内容でダウンロードを行う場合はどのようにしたらいいでしょうか。
サイトを参考に色々やってみていますが実現できていません。ご教示お願いできないでしょうか
参考にさせていただいたサイトは
http://qiita.com/satzz/items/250c49ce22f082919e02
https://blog.hello-world.jp.net/ruby/1335/
です
csvダウンロードアクションを作成
collection_action :download_report, :method => :get do
posts = Post.order('created_at DESC') ここを画面検索条件で絞り込んだインスタンスにしたい
csv = CSV.generate do |csv|
csv << ['id', 'title', 'Author']
posts.each do |post|
csv << [
post.id,
post.title,
post.author.full_name
]
end
end
# Shift-JISでcsvを出力
send_data csv.encode('Shift_JIS', :invalid => :replace, :undef => :replace), type: 'text/csv; charset=shift_jis; header=present', disposition: "attachment; filename=report.csv"
end
csvダウンロードリンクを追加
action_item(:csv_report, only: :index) do
link_to('csv report', params.merge(:action => :download_report))
end
あなたの回答
tips
プレビュー