如何在Active Admin自定义批处理操作中重新加载代码?

时间:2016-02-29 22:31:57

标签: ruby-on-rails activeadmin

我在Rails应用程序中使用自定义批处理操作的活动管理员。 批处理操作是从数据库的最后5条记录创建的。请在下面找到附件代码。

但是,当创建新记录(事件)时,批处理操作不会刷新。我想知道如何强制刷新?是否有一个函数可以调用以从新记录中刷新批处理操作?感谢

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

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

2 个答案:

答案 0 :(得分:1)

你想要的方式无法工作,因为代码只在Rails / ActiveAdmin启动时执行一次。

但还有另一种方法可以去:

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

代码没有经过我的测试,但这个想法应该有效。

答案 1 :(得分:0)

对我有用的方法(而不是尝试强制执行refresh / attach_to_event)是在每次加载时重新计算,将lambda作为form的值传递。 有关详细信息,请参见:ActiveAdmin Batch Action Dynamic Form