LoginSignup
9
8

More than 5 years have passed since last update.

ActiveAdminでカスタム一括処理を入れる

Last updated at Posted at 2018-07-25

この記事は

  • Railsの便利なDB管理ツールActiveAdminで独自の一括処理を入れるメモです

やりたいこと

  • ActiveAdminではデフォルトで一覧画面からレコードの一括削除が実施できます
  • 削除ではなく、あるカラムを一括更新したかったです

やり方

  • まずindexアクションをオーバーライドしている場合はselectable_columnを明記して列の1番左側にチェックボックスを配置します
  • indexをオーバーライドしていなければ自動で出るので対処は不要です
index do
  selectable_column
  id_column
  column 'ユーザーID', :user_id
...
end
  • 独自更新の処理を下記のように書きます
  • すると「今日の日付を入れる」というのが一覧画面の一括操作のプルダウンに表示されます
  batch_action '今日の日付を入れる' do |ids|
    batch_action_collection.find(ids).each do |record|
      record.some_date_column = Time.now
      record.save!
    end
    redirect_to collection_path, alert: "今日の日付に更新しました"
  end

終わりに

  • ActiveAdmin便利だなー
9
8
0

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
9
8