LoginSignup
5
5

More than 5 years have passed since last update.

ActiveAdminのCSVダウンロードをカスタマイズする

Last updated at Posted at 2016-09-22

ActiveAdminのリソースのindexには、モデルの全データをCSVダウンロードするボタンがついてくるが、行や列を絞ったCSVをダウンロードするボタンを追加してみる。(結果的には、CSVを一行ずつ組み立てるだけ)

ActiveAdmin.register Klass do
  before_filter :skip_sidebar!

  collection_action :download_report, method: :get do
    query = Klass.get_query
    csv_data = CSV.generate do |csv|
      csv << %w{col1 col2}
      query.each do |row|
        csv << [row.col1, row.col2]
      end
    end
    send_data csv_data, type: 'text/csv; header=present', disposition: 'attachment; filename=file.csv'
  end

  action_item only: :index do
    link_to 'download report', params.merge(action: :download_report)
  end

  index download_links: false do
    # ...
  end
end

参考

以下をコピーして修正した

5
5
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5