简体   繁体   中英

How to reload code in Active Admin Custom Batch Actions?

I use active admin in a Rails app with custom batch actions. The batch actions are created from the last 5 records from the database. Please find attached the code below.

However when a new record (Event) is created the batch actions do not refresh. I would like to know how to force a refresh? Is there a function I can call to make the batch actions refresh from new records? Thanks

ActiveAdmin.register TimeLineMessage do
  menu
  menu label: 'Rundown page'

  Event.order("created_at DESC").limit(5).reload.each do |event|
    batch_action ("Move to " + event.name.to_s).to_sym do |ids|
      TimeLineMessage.find(ids).each do |tlm|
        tlm.event_id = event.id
        tlm.save
      end
      redirect_to collection_path, alert: "The content tiles have been moved to "+ event.name.to_s + " event "
    end
  end

Ref: http://activeadmin.info/docs/9-batch-actions.html

The way you want to go can't work, because of the fact that the code is only executed one time at the boot of Rails/ActiveAdmin.

But there is a other way that you can go:

batch_action :attach_to_event, form: {
  event_id: -> { Event.order("created_at DESC").limit(5).pluck(:id, :name) }
} do |ids, inputs|
  event = Event.find(inputs[:event_id])
  TimeLineMessage.find(ids).each do |tlm|
    tlm.event_id = event.id
    tlm.save
  end
  redirect_to collection_path, alert: "The content tiles have been moved to "+ event.name.to_s + " event "
end

The code isn't tested by me, but the idea should work.

The approach that worked for me instead of trying to force a refresh/attach_to_event is to re-calculate on each load, pass a lambda as the value of form. For details see : ActiveAdmin Batch Action Dynamic Form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM