Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails & ActiveAdmin - flash.now on custom pages

Has anyone had success implementing flash.now[:notice] alerts within a custom page in ActiveAdmin?

I am using a custom page created with ActiveAdmin.register_page "CustomPage".

flash[:notice] works, but I cannot use it because I am not using redirect_to so the alerts show on the incorrect page.

My Gemfile contains gem 'activeadmin', github: 'activeadmin'

app/admin/test.rb

ActiveAdmin.register_page "Test" do 
  content do 
    flash.now[:notice] = 'Test'
  end
end
like image 245
kayatela Avatar asked Sep 03 '25 07:09

kayatela


1 Answers

Setting the flash.now[:notice] inside the content block is too late for it to be evaluated and rendered as part of the custom page. Instead you can set the flash message in a before_action in the controller:

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

See the filters section of the Action Controller Overview guide for more detail on before_action.

like image 157
Charles Maresh Avatar answered Sep 04 '25 22:09

Charles Maresh