Introduction:

Rails Admin is a Gem that creates an interface for you to manage your data. For further explanation take a look at the Gem’s page on GitHub!

Start by:

  1. On your gemfile: gem 'rails_admin'

  2. Run bundle install

  3. Restart your Rails server.

  4. Run rails generate rails_admin:install

  5. Create an admin user with rails g devise Admin

  6. Create a namespace under your routes:

    1. https://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing for how to do that!

    2. We will instead follow the rails admin installation guide by adding this line to routes.rb below devise_for :admin

      mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
      
  7. Authenticate the admin view by uncommenting and replace user with admin in config/initializers/rails_admin.rb

    config.authenticate_with do
      warden.authenticate! scope: :admin
    end
    config.current_user_method(&:current_admin)
    
  8. http://localhost:3000/admin Check out your tables!

  9. It's really complete! You can create data, update data, and delete data in all of your tables

Actions:

  1. Let's limit the access to certain models using admin!

    In rails_admin.rb

    config.actions do
      edit do
        only ['post', 'user']
      end
      delete do
        except ['like']
      end
    end
    
  2. Re run your admin see what changed!

  3. Put something like this this in your model class (be sure to rename things appropriately):

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :likes
  has_many :users_liked, through: :likes, source: :user

  validates :caption, presence: true

  rails_admin do
    configure :user do
      label 'Owner of this post: '
    end
  end
end

Navigation: