参考:
http://dmitrypol.github.io/2015/09/10/rails-admin.html
http://fernandomarcelo.com/2012/05/rails-admin-creating-a-custom-action/
问题
代码实现
lib/rails_admin_course_open.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'
module RailsAdminCourseOpen
end
module RailsAdmin
module Config
module Actions
class CourseOpen < RailsAdmin::Config::Actions::Base
# There are several options that you can set here.
# Check https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/base.rb for more info.
register_instance_option :bulkable? do
true
end
register_instance_option :controller do
Proc.new do
# Get all selected rows
@objects = list_entries(@model_config, :destroy)
# Update field open to true
@objects.each do |object|
object.update_attribute(:open, true)
end
flash[:success] = "#{@model_config.label} successfully opend."
redirect_to back_or_index
end
end
end
class CourseClose < RailsAdmin::Config::Actions::Base
# There are several options that you can set here.
# Check https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/base.rb for more info.
register_instance_option :bulkable? do
true
end
register_instance_option :controller do
Proc.new do
# Get all selected rows
@objects = list_entries(@model_config, :destroy)
# Update field open to true
@objects.each do |object|
object.update_attribute(:open, false)
end
flash[:success] = "#{@model_config.label} successfully closed."
redirect_to back_or_index
end
end
end
end
end
endconfig/initializers/rails_admin.rb 注册
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44RailsAdmin.config do |config|
# Register the class in lib/rails_admin_publish.rb
module RailsAdmin
module Config
module Actions
class CourseOpen < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
end
class CourseClose < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
end
end
end
end
config.actions do
# root actions
dashboard # mandatory
# collection actions
index # mandatory
new
export
history_index
bulk_delete
# member actions
show
edit
delete
history_show
# show_in_app
course_open do
# Make it visible only for Course model. You can remove this if you don't need.
visible do
bindings[:abstract_model].model.to_s == "Course"
end
end
course_close do
# Make it visible only for Course model. You can remove this if you don't need.
visible do
bindings[:abstract_model].model.to_s == "Course"
end
end
end
endconfig/locales/zh-CN.yml
1
2
3
4
5
6
7actions:
course_open:
menu: "开启选课"
bulk_link: "开启选课%{model_label_plural}"
course_close:
menu: "关闭选课"
bulk_link: "关闭选课%{model_label_plural}"