Updated

Introduction

Administrate is a great framework for creating flexible, powerful admin dashboards in Rails. I tried Rails Admin before, but I found the DSL too confusing for even the simplest form.

Administrate has no DSL and follows the standard Rails constroller-view. Therefore if you need something special you just override the defaults and add your functionality.

In this tutorial we will solve some installation and configuration issues.

Install

Add gem in your Gemfile, plus bourbon since administrate has it as a dependency

# Gemfile 
gem 'administrate'
gem 'bourbon'

Then run

rails generate administrate:install

Restart your server, and visit http://localhost:3000/admin to see your new dashboard.

will_paginate and kaminari

To avoid undefined method erros if you are using will_paginate gem in your project, create an initializer to handle conflicting kaminari methods, since administrate has kaminari as a dependency.

# an initializer to handle conflicting kaminari and will_paginate methods 
# config/initializers/will_paginate.rb
# https://github.com/thoughtbot/administrate/issues/284

Kaminari.configure do |config|
  config.page_method_name = :per_page_kaminari
end

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        def per(value = nil) per_page(value) end
        def total_count() count end
        def first_page?() self == first end
        def last_page?() self == last end
      end
    end
    module CollectionMethods
      alias_method :num_pages, :total_pages
    end
  end
end

Friendly ID

If your are using friendly_id gem uncoment the folowing

# app/controllers/admin/your_model_controller.rb
# Define a custom finder by overriding the `find_resource` method: 
def find_resource(param) 
    YourModel.find_by!(slug: param) 
end

Secure your admin page

To allow only admin user to view the dashboard, define a custom authenticate_admin as follows

module Admin
 class ApplicationController < Administrate::ApplicationController
 before_action :authenticate_admin

 def authenticate_admin
     return if current_user && current_user.admin
     redirect_to(request.referrer || root_path)
 end 
end

Show paperclip image

Add the following gem in your Gemfile

gem "administrate-field-image"

Then assuming your attachment is named image, add the following line to your model dashboard

# app/dashboards/your_model_dashboard.rb

 ATTRIBUTE_TYPES = {
    ...
    image:  Field::Image,
    ...
}

How to update Rails store field

Define a custom resource_params function in your admin model controller

# app/controllers/admin/your_model_controller.rb
def resource_params
      store_hash = JSON.parse(params[:resource_name]
                       .try(:fetch, :store_name, {})
                       .gsub('=>',':'))

      params[:resource_name][:store_name] = store_hash

      params.require(:resource_name).tap do |whitelisted|
           whitelisted[:store_name] = params[:resource_name][:store_name]
      end
end