Last Updated: September 09, 2019
·
7.965K
· dalpo

How to create a custom section for RailsAdmin Engine

Sections describe different views in the RailsAdmin engine.
Theese are very useful if you are writing a reusable custom action.

Each section's class object can store generic configuration about that section (such as the number of visible tabs in the main navigation), while the instances (accessed via model configuration objects) store model specific configuration (such as the visibility of the model).

With the following lines you could easily write CustomAction configuration (aka Section):

require 'rails_admin/config/sections/base'

module RailsAdmin
  module Config
    module Sections
      # Configuration of the clone action
      class SampleSection < RailsAdmin::Config::Sections::Base

        register_instance_option :sample_conf do
          nil # set a default value here
        end

        register_instance_option :another_sample_conf do
          nil # set a default value here
        end

      end
    end
  end
end

So you can specify customize configurations in the rails_admin configuration initializer:

RailsAdmin.config do |config|
  config.model 'Team' do
    sample_section do
      custom_method :some_value

      another_sample_conf do
        bindings[:object].do_something!
      end
    end
  end
end

Every value of your Section could be retrieved with the model_config object in your custom action.
For instance:

require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'

module RailsAdmin
  module Config
    module Actions
      class SampleCustomAction < Base
        RailsAdmin::Config::Actions.register(self)

        register_instance_option :controller do
          Proc.new do
            # retrieve configuration: 
            model_config.sample_section.sample_conf
            model_config.sample_section.another_sample_conf

           # do something...

            respond_to do |format|
              format.html { render @action.template_name }
              format.js   { render @action.template_name, :layout => false }
            end

          end
        end
      end
    end
  end
end

Important note:

When you define a custom section this must be initialized before rails_admin to work correctly. So in your Gemfile you must define rails_admin after your custom action gem.

gem 'rails_admin_custom_action'
gem 'rails_admin'

1 Response
Add your response

Hello, I tried to add a custom section that inherits from edit. My purpose is to make a similar to the view "create" (form), but I want some custom things to do after the model is created and working inside the model with "after_create" gives me some trouble.

I have followed your steps to add the section, but when I try to define the configuration inside my model, to select which labels to add to the "form" of my "section", it says that the section does not exist.

How do I tell to rails_admin to look at my custom section?

over 1 year ago ·