我一直在阅读有关自定义操作的rails_admin wiki部分,但我对rails_admin完全不熟悉,这个文档对我来说有点混乱。
我需要实现的是,当管理员用户单击选项以删除特定的员工(员工是我的应用程序中的模型)时,实际删除给定员工的代码不能是rails_admin删除的默认方式,但是我需要以某种方式提供给rails_admin的给定代码块。
请注意,我仍然希望以通常的方式删除通过rails_admin访问的其余数据模型。只有员工模型需要具有自定义删除例程。
答案 0 :(得分:10)
我刚遇到同样的问题,因为我需要将用户标记为删除而不是立即删除。
在黑客攻击之后我终于找到了一种方法:重写RailsAdmin的默认删除操作。
这是我添加自己的自定义代码后的删除操作(不是删除,而是使用自定义通知标记删除,如果用户已标记为删除,则隐藏删除按钮)。 :
# config/initializers/rails_admin_delete_override.rb
module RailsAdmin
module Config
module Actions
class Delete < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
register_instance_option :member do
true
end
register_instance_option :route_fragment do
'delete'
end
register_instance_option :http_methods do
[:get, :delete]
end
register_instance_option :authorization_key do
:destroy
end
register_instance_option :visible? do
bindings[:object].class.base_class.name != 'User' || !bindings[:object].to_destroy?
end
register_instance_option :controller do
Proc.new do
if request.get? # DELETE
respond_to do |format|
format.html { render @action.template_name }
format.js { render @action.template_name, :layout => false }
end
elsif request.delete? # DESTROY
redirect_path = nil
@auditing_adapter && @auditing_adapter.delete_object(@object, @abstract_model, _current_user)
if @object.class.base_class.name == 'User'
@object.to_destroy!
flash[:success] = t("admin.flash.user_destroy_successful", :name => @model_config.label)
redirect_path = index_path
else
if @object.destroy
flash[:success] = t("admin.flash.successful", :name => @model_config.label, :action => t("admin.actions.delete.done"))
redirect_path = index_path
else
flash[:error] = t("admin.flash.error", :name => @model_config.label, :action => t("admin.actions.delete.done"))
redirect_path = back_or_index
end
end
redirect_to redirect_path
end
end
end
register_instance_option :link_icon do
'icon-remove'
end
end
end
end
end
您可以在此处找到原始操作代码:https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/delete.rb
答案 1 :(得分:0)
经过数小时的黑客攻击后,我找到了一种更简单的自定义操作方式。 您所需要的只是创建一个不同的模块名称(在我的情况下是&#39; MyRailsAdmin&#39;),然后扩展原始的动作类,例如。 &#39;删除&#39;
# config/initializers/rails_admin_delete.rb
require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'
module MyRailsAdmin
module Config
module Actions
class Delete < RailsAdmin::Config::Actions::Delete
RailsAdmin::Config::Actions::register(self)
register_instance_option :controller do
proc do
if request.get? # DELETE
respond_to do |format|
format.html { render @action.template_name }
format.js { render @action.template_name, layout: false }
end
elsif request.delete? # DESTROY
redirect_path = nil
@auditing_adapter && @auditing_adapter.delete_object(@object, @abstract_model, _current_user)
# [CUSTOM DELETE] mark as deleted (status = 0) rather than actually hard delete it
@object.status = 0
if @object.save
flash[:success] = t('admin.flash.successful', name: @model_config.label, action: t('admin.actions.delete.done'))
redirect_path = index_path
else
flash[:error] = t('admin.flash.error', name: @model_config.label, action: t('admin.actions.delete.done'))
redirect_path = back_or_index
end
redirect_to redirect_path
end
end
end
end
end
end
end