Git Product home page Git Product logo

rails-active-admin-readme's Introduction

Administration With ActiveAdmin

Objectives

  1. Use the ActiveAdmin gem to add admin features to the blog app.
  2. Customize the ActiveAdmin resource to prevent certain actions and hide form fields.

Lesson

We're going to use ActiveAdmin to add administrative features to our blog application. Not anyone should just be able to come in and create a new author on our blog. That's something only an admin should do.

Our app already has Devise set up for user management. We're going to leave our main blog pages open to the public and only password-protect the ActiveAdmin parts.

Setting Up ActiveAdmin

To get started, let's add ActiveAdmin to our Gemfile:

gem 'activeadmin', github: 'activeadmin'

We need the github: 'activeadmin' part to use the latest under-development version, which is compatible with Rails 4.2.

Then run bundle install.

Note: We're going to follow along with the github docs, because the ActiveAdmin website is out-of-date.

Now we generate the ActiveAdmin installation with rails g active_admin:install. This will create a migration for an AdminUser model, add a default user to our seeds.rb file, and generate the base ActiveAdmin files.

Let's run rake db:migrate then rake db:seed to finish the setup.

Finally, restart your Rails server, then browse to http://localhost:3000/admin.

We have a login page! Log in as [email protected] with password password and you'll be on the ActiveAdmin dashboard.

Top-tip: You can change the login and password in seeds.rb before migrating, or even better, log in and change the login/password from within the app to make it more secure.

Registering Models

Okay, we have a dashboard, but there's not much to do. That's because we haven't registered any models yet.

Let's set this up to administer authors.

rails generate active_admin:resource Author

This will create a new file at app/admin/author.rb. We're not even gonna look at it yet. Trust me. Look away!

look away

Just reload /admin and you should see a new item in the top bar for Authors. Click it. You can add new authors, edit and delete existing ones, all from a single command and no code!

joey shock

Customizing ActiveAdmin Resources

Out of the box it's incredibly easy to set up ActiveAdmin to provide admin features. But you might want to give some restrictions to what it can do.

First, let's prevent authors from being deleted, even by ActiveAdmin. Now you can look at app/admin/author.rb.

We want to make sure we're set up to use Strong Params to only allow certain fields, so let's set that up:

# admin/author.rb

ActiveAdmin.register Author do
    permit_params :name, :genre
end

Note: In some instances, you will be required to set permit_params even if you don't want to change any of them from the default. If you receive an ActiveModel::ForbiddenAttributes exception when working with ActiveAdmin, set up those permit_params.

Now we're only allowing name and genre to be set.

By default, all CRUD actions are available to an ActiveAdmin resource. But we can change that:

# admin/author.rb

ActiveAdmin.register Author do

  permit_params :name, :genre
  actions :all, except: [:destroy]

end

If we refresh the authors admin page, the "Delete" link is now gone.

If we want to edit the elements on the form, and remove bio so that the form matches our permit_params, we can override the default form:

# admin/author.rb

  permit_params :name, :genre
  actions :all, except: [:destroy]

  form do |f|
    inputs 'Author' do
      f.input :name
      f.input :genre
    end
    f.semantic_errors
    f.actions
  end

Reload again and bio is gone. This form do ... end block uses an ActiveAdmin domain-specific language (DSL) to manipulate a Formtastic form. Removing it completely returns the form to the default.

Advanced: You might be tempted to think of admin/author.rb as if it were a kind of model, but as you can see, it controls all aspects of an ActiveAdmin resource, including both business logic and presentation logic. One could make the argument that this is a violation of Separation of Concerns, but in Rails a "resource" is a concept that comprises model, controller, and view, so in that sense, an ActiveAdmin's resource is concerned with all aspects of that resource. In any case, it's easy to find where the code goes.

Summary

We've seen how easy it is to use ActiveAdmin to quickly add administration to our app, and how to do some basic customization. More configuration options are available and can be found in the docs, but I think we can all agree that the default options are very helpful and easy to use.

friends clap

rails-active-admin-readme's People

Contributors

annjohn avatar bhollan avatar blake41 avatar pletcher avatar scottcreynolds avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-active-admin-readme's Issues

activeadmin requires jquery-rails 4.2.0

Bundler could not find compatible versions for gem "jquery-rails":
  In snapshot (Gemfile.lock):
    jquery-rails (= 4.0.5)

  In Gemfile:
    jquery-rails

    activeadmin was resolved to 2.0.0.alpha, which depends on
      jquery-rails (>= 4.2.0)

Running bundle update will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

editing Gemfile to specify jquery-rails version fixed the issue for me:

gem 'jquery-rails', '~> 4.2.0'

admin/authors.rb, not author.rb

It says in the readme: "You might be tempted to think of admin/author.rb as if it were a kind of model..." - it may be a version issue, but actually the name of the file we are manipulating is admin/authors.rb in plural, unlike models/author.rb in singular, so there's no temptation to mistake one for the other.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.