Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin - blank slate - customized message - Rails

How can i change the blank_slate message on ActiveAdmin. Each of my models would have a different blank_slate message.

Example:

  • Transport: No transports, do this and this
  • Car: Did you forget, no cars until this
like image 703
zPrima Avatar asked Oct 26 '25 09:10

zPrima


2 Answers

You can use (poorly documented but not so monkey-path) PagePresenter.blank_slate_link option responsible for rendering a link to new resource on an empty index page (see GitHub source). You may override it in your ActiveAdmin's resource index page using blank_slate_link keyword in the header of the index page definition. E.g. (app/admin/transport.rb):

ActiveAdmin.register Transport do
  # Some cool initialization: scope, filters, ...
  index blank_slate_link: 
        -> { "No transports, "+
             link_to("do this and this", new_admin_transport_path) }
  do
    # ...other index page details go here...
  end
  # ...rest of pages: show, edit, ...
end

This way you get as sophisticated link as you want. And the I18n's active_admin.blank_slate.link is no longer used for generating link to a new resource.

But as you see render_blank_slate still uses the hard-coded active_admin.blank_slate.content string to render text right before the link (e.g., "There are no %{resource_name} yet."). If you want to get rid of this text you should override it in your YAML localization file. You may simply leave it empty (app/config/locales/en.yml):

en:
  active_admin:
    blank_slate:
      content: ""

Or, in your case I'd suggest:

en:
  active_admin:
    blank_slate:
      content: "%{resource_name}: "

which gives you the desired "Transport: No transports, do this and this" message.

like image 176
Jakub Avatar answered Oct 29 '25 00:10

Jakub


You can monkey patch ActiveAdmin to load the message as desired:

require 'active_admin/helpers/collection'

module ActiveAdmin
  module Views
    module Pages
      class Index < Base
        protected
        def render_blank_slate
          # for example only, you can define your own I18n structure
          # You can use active_admin_config.resource_label too if not mistaken
          blank_slate_content = I18n.t("active_admin.blank_slate.content.#{active_admin_config.plural_resource_label}")
          insert_tag(view_factory.blank_slate, blank_slate_content)
        end
      end
    end
  end
end

load this file to the initializer folder and put the messages in your language YAML file following the structure as defined in the blank_slate_content assignment line.

like image 34
nayiaw Avatar answered Oct 28 '25 22:10

nayiaw