Cheat Sheet速查表
跳转到主要内容

ActiveAdmin 速查表

本文根据 Devhints / ActiveAdmin 翻译。代码示例及版本范围沿用原文。

列表作用域 (Listing scopes)

允许你根据特定作用域筛选列表项。

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

侧边栏筛选器

filter :email
filter :username

自定义操作

你可以为模型定义自定义操作。

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

创建路由

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

在索引页 (index) 中添加链接

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

并在详情页 (show) 或编辑页 (edit) 中添加链接

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

列 (Columns)

column :foo
column :title, sortable: :name do |post|
  strong post.title
end

其他辅助工具

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

禁用“新建文章”功能

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