Skip to content
Mitsuhiro Shibuya edited this page Sep 18, 2022 · 39 revisions

RailsAdmin provides its out of the box administrative interface by inspecting your application's models and following some Rails conventions. For a more tailored experience, it also provides a configuration DSL which allows you to customize many aspects of the interface.

Asset delivery

Rails 7.0 drastically changed the way it integrates with JavaScript and CSS. RailsAdmin is made to be compatible with the change and it supports multiple asset delivery methods that are described in this section.

For setting up the asset delivery, the RailsAdmin installer will basically auto-detect the asset delivery method and perform necessary configurations depending on the method it choose. But you can also manually specify the method to use.

$ rails g rails_admin:install --asset=sprockets

Once installation is finished, you can find the current asset delivery method in config/initializers/rails_admin.rb.

RailsAdmin.config do |config|
  config.asset_source = :sprockets
end

Sprockets (a.k.a. Asset Pipeline)

If your application was built on top of the Sprockets-based asset pipeline, this is the way to go.

$ rails g rails_admin:install --asset=sprockets

RailsAdmin will load app/assets/javascripts/rails_admin/application.js and app/assets/stylesheets/rails_admin/application.css via the asset pipeline. Since these files are already included in RailsAdmin, no further setup is needed in your Rails application.

Webpacker

If you have a pre-Rails7 application which have Webpacker installed, this is the option.

$ rails g rails_admin:install --asset=webpacker

Following files will be created in your application to be picked up by Webpacker:

  • app/javascript/packs/rails_admin.js - JavaScript file to be used by RailsAdmin
  • app/javascript/stylesheets/rails_admin.scss - SCSS file to build the RailsAdmin stylesheet

ImportMap

Use this if your application is configured to use ImportMap by using the gem importmap-rails, which became default from Rails 7.0.

$ rails g rails_admin:install --asset=importmap

Following files will be created in your application:

  • app/javascript/rails_admin.js - JavaScript file to be loaded by RailsAdmin via ImportMap
  • app/assets/stylesheets/rails_admin.scss - SCSS file to build the RailsAdmin stylesheet by using cssbundling-rails
  • config/importmap.rails_admin.rb - ImportMap configuration used to load RailsAdmin JavaScript dependencies

Webpack

You can choose to use webpack (not webpacker) via the jsbundling-rails gem.

$ rails g rails_admin:install --asset=webpack

Following files will be created in your application:

  • app/javascript/rails_admin.js - JavaScript file to be compiled with Webpack
  • app/assets/stylesheets/rails_admin.scss - SCSS file to build the RailsAdmin stylesheet by using cssbundling-rails

Following file will be modified when successful, or RailAdmin will instruct you on how to change:

  • webpack.config.js - the configuration entry is added to let Webpack compile the RailsAdmin JavaScript asset

Set the application name

RailsAdmin.config do |config|
  config.main_app_name = ["Cool app", "BackOffice"]
  # or something more dynamic
  config.main_app_name = Proc.new { |controller| [ "Cool app", "BackOffice - #{controller.params[:action].try(:titleize)}" ] }
end

Locale

To set locale for RailsAdmin - use a custom parent controller and add an around_action to execute the request in the context of a given locale, ie.

# initializers/rails_admin.rb
RailsAdmin.config do |config|
  config.parent_controller = "Admin::BaseController"
end

# controllers/admin/base_controller.rb
class Admin::BaseController < ActionController::Base
  around_action :use_default_locale
  
  private
  
  def use_default_locale(&block)
    # Executes the request with the I18n.default_locale.
    # https://github.com/ruby-i18n/i18n/commit/9b14943d5e814723296cd501283d9343985fca4e
    I18n.with_locale(I18n.default_locale, &block)
  end
end

Related Rails Guides

Authentication integration (Devise, Sorcery, Manual)

Authentication

Authorization (Cancancan, Pundit)

Authorization

Instance labels

config.label_methods << :description # Default is [:name, :title]

Browser validations

config.browser_validations = false # Default is true

Next

Then you can start adding actions, configuring models, sections and fields.

Clone this wiki locally