github: https://github.com/activeadmin/activeadmin
官方文檔:https://activeadmin.info/index.html
安裝
devise是用于登錄驗證
gem 'activeadmin'
gem 'devise'
由于我們使用devise浴滴,所以我們需要運行這個構(gòu)造器
rails g active_admin:install
執(zhí)行后會創(chuàng)建這些文件
app/admin/dashboard.rb
app/assets/javascripts/active_admin.js
app/assets/stylesheets/active_admin.scss
config/initializers/active_admin.rb
終端執(zhí)行
bin/rails db:migrate
activeAdmin 會在seeds.rb中生成默認用戶的創(chuàng)建方式,終端執(zhí)行下面命令來生成后臺用戶
bin/rails db:seed
然后就可以訪問了,會讓你登錄纬纪,就用剛生成的用戶的信息
http://192.168.20.106:3000/admin/
如果要關(guān)閉用戶登錄驗證功能,可以用:
initializer/active_admin
config.current_user_method = false
config.authentication_method = false
Comment
因為不需要comment(注解)這個功能,所以在initializer中的active_admin中關(guān)閉這個功能
config.comments = false
Utility Navigation(導(dǎo)航)
在右上角有一些快速導(dǎo)航按鈕浦妄,默認的有兩個,一個是當(dāng)前的admin用戶见芹,另外一個是登出按鈕剂娄,我們可以增加導(dǎo)航按鈕
ActiveAdmin.setup do |config|
config.namespace :admin do |admin|
admin.build_menu :utility_navigation do |menu|
menu.add label: "ActiveAdmin.info", url: "http://www.activeadmin.info",
html_options: { target: :blank }
admin.add_current_user_to_menu menu
admin.add_logout_button_to_menu menu
end
end
end
創(chuàng)建Resources
eg: Post是我的一個model
bin/rails g active_admin:resource Post
執(zhí)行后會生成app/admin/post.rb文件
permit_params
使用permit_params方法來定義哪些屬性可以被修改,如果修改了沒有被permit_params定義的屬性,修改不會生效
ActiveAdmin.register Post do
permit_params :title, :content, :publisher_id
end
定義一個Resource中的action
默認的辆童,所有的CRUD的actions都可以操作宜咒,如果要禁用其中的某些操作惠赫,用如下定義:
ActiveAdmin.register Post do
actions :all, except: [:update, :destroy]
end
自定義Menu
一個resource默認都會在全局導(dǎo)航欄上把鉴,如果要禁止一個resource出現(xiàn)在全局導(dǎo)航欄上,我們可以用:
ActiveAdmin.register Post do
menu false
end
menu label: "自定義標(biāo)題",priority: 1
menu有4個選項可以用:
:label - 指定該menu的名稱
:parent -指定一個頂級菜單
:if -指定一個條件
:priority - 默認為10儿咱,在全局導(dǎo)航欄上的位置
添加自定義的menu
config/initializers/active_admin.rb
config.namespace :admin do |admin|
admin.build_menu do |menu|
menu.add label: "The Application", url: "/", priority: 0
menu.add label: "Sites" do |sites|
sites.add label: "Google",
url: "http://google.com",
html_options: { target: :blank }
sites.add label: "Github",
url: "http://github.com"
end
end
end
自定義resource的index頁面
提供4中樣式庭砍,Block,Blog,Grid,Table,一般我們用的是table,也是默認的樣式混埠。這里只介紹table.
默認的怠缸,index頁面會展示對應(yīng)的model的所以字段,可供查看钳宪,修改揭北,刪除。
如果要自定義字段吏颖,可以用:
index do
selectable_column
column :title
column "My custom name" ,:name
end
這些自定的字段要被index包裹
selectable_column 顯示勾選框搔体,用于批量操作
column 顯示什么字段,該字段必須是model的字段名半醉,可以給這個字段另取一個別名
還可以根據(jù)條件設(shè)置特別的row樣式疚俱,這個some_style樣式放在app/asset/stylesheets/active_admin.scss中
index(row_class: ->item { 'some_style' if item.is_show }) do
end
自定義Actions
如果要給resource設(shè)置鏈接到查看,修改缩多,刪除這幾個按鈕呆奕,可以用actions方法
index do
selectable_column
column :title
actions
end
也可以增加自定的鏈接
index do
selectable_column
column :title
actions do |post|
item "Preview", admin_preview_post_path(post), class: "member_link"
end
end
獲取重新設(shè)置鏈接
index do
column :title
actions defaults: false do |post|
item "View", admin_post_path(post)
end
end
Form(model的創(chuàng)建或者修改表單)
默認的表單是展示所有的字段
form do |f|
f.semantic_errors # shows errors on :base
f.inputs # builds an input field for every attribute
f.actions # adds the 'Submit' and 'Cancel' buttons
end
示例:
form title: 'A custom title' do |f|
inputs 'Details' do
input :part1
input :part2, label: "Publish Post At"
li "Created at #{f.object.created_at}" unless f.object.new_record?
input :part3
end
panel 'Markup' do
"The following can be used in the content below..."
end
inputs 'Content', :part3
para "Press cancel to return to the list without saving."
input :created_at, as: :datepicker,
datepicker_options: {
min_date: "2013-10-8",
max_date: "+3D"
}
actions
end
對應(yīng)的圖示:
增加action的按鈕
link_to path 中的path指定的方式是
- 命名空間_model的復(fù)數(shù)形式_path
- controller名_方法名_path
action_item only: :index do
link_to '刷新疑似狀態(tài)', admin_app_oral_memories_path, notice: "刷新成功"
end
增加batch操作按鈕
batch_action "封禁" do |selection|
end
Index Filter 過濾器
filter :name, label: "name"
一般的养晋,通過上面這個形式就好了,activeadmin會自動判斷其類型梁钾。
或者有另個情況绳泉,有個關(guān)聯(lián)表。
filter :location_id, label: "地點", as: :select, collection: AppExamLocation.all.map{|x| [x.location,x.id]}
通過:as可以指定類型姆泻,提供這一下這幾種類型
:string - A drop down for selecting “Contains”, “Equals”, “Starts with”, “Ends with” and an input for a value.
:date_range - A start and end date field with calendar inputs
:numeric - A drop down for selecting “Equal To”, “Greater Than” or “Less Than” and an input for a value.
:select - A drop down which filters based on a selected item in a collection or all.
:check_boxes - A list of check boxes users can turn on and off to filter