DEV Community

Lam
Lam

Posted on

Quick Activeadmin Cheat Sheet

See more:

Disabling 'new post'

ActiveAdmin.register Post do
  actions :index, :edit
  # or: config.clear_action_items!
end
Enter fullscreen mode Exit fullscreen mode

Other helpers

status_tag "Done"           # Gray
status_tag "Finished", :ok  # Green
status_tag "You", :warn     # Orange
status_tag "Failed", :error # Red
Enter fullscreen mode Exit fullscreen mode

Columns

column :foo
Enter fullscreen mode Exit fullscreen mode
column :title, sortable: :name do |post|
  strong post.title
end
Enter fullscreen mode Exit fullscreen mode

Custom actions

You can define custom actions for models.
{: .-setup}

before_filter only: [:show, :edit, :publish] do
  @post = Post.find(params[:id])
end
Enter fullscreen mode Exit fullscreen mode

Make the route

member_action :publish, method: :put do
  @post.publish!
  redirect_to admin_posts_path, notice: "The post '#{@post}' has been published!"
end
Enter fullscreen mode Exit fullscreen mode

Link it in the index

index do
  column do |post|
    link_to 'Publish', publish_admin_post_path(post), method: :put
  end
end
Enter fullscreen mode Exit fullscreen mode

And link it in show/edit

action_item only: [:edit, :show] do
  @post = Post.find(params[:id])
  link_to 'Publish', publish_admin_post_path(post), method: :put
end
Enter fullscreen mode Exit fullscreen mode

Sidebar filters

filter :email
filter :username
Enter fullscreen mode Exit fullscreen mode

Listing scopes

Allows you to filter listings by a certain scope.
{: .-setup}

scope :draft
scope :for_approval
Enter fullscreen mode Exit fullscreen mode
scope :public, if: ->{ current_admin_user.can?(...) }
scope "Unapproved", :pending
scope("Published") { |books| books.where(:published: true) }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)