Rails & ActiveAdmin - flash.now 在自定义页面上

Rails & ActiveAdmin - flash.now on custom pages

有没有人在 ActiveAdmin 的自定义页面内成功实施 flash.now[:notice] 警报?

我正在使用使用 ActiveAdmin.register_page "CustomPage" 创建的自定义页面。

flash[:notice] 有效,但我无法使用它,因为我没有使用 redirect_to,所以警报显示在错误的页面上。

我的 Gemfile 包含 gem 'activeadmin', github: 'activeadmin'

app/admin/test.rb

ActiveAdmin.register_page "Test" do 
  content do 
    flash.now[:notice] = 'Test'
  end
end

content 块中设置 flash.now[:notice] 太晚了,无法将其作为自定义页面的一部分进行评估和呈现。相反,您可以在控制器的 before_action 中设置 flash 消息:

ActiveAdmin.register_page "Test" do
  content do
    # Test content
  end

  controller do
    before_action :set_notice, only: :index

    private

    def set_notice
      flash.now[:notice] = 'Test'
    end
  end
end

有关 before_action 的更多详细信息,请参阅动作控制器概述指南的 filters 部分。