【发布时间】:2011-10-12 10:50:00
【问题描述】:
在 rails gem active admin 我想从 default_actions 中删除删除选项,而我仍然需要编辑和显示操作,有什么办法吗?
【问题讨论】:
标签: ruby-on-rails rubygems activeadmin
在 rails gem active admin 我想从 default_actions 中删除删除选项,而我仍然需要编辑和显示操作,有什么办法吗?
【问题讨论】:
标签: ruby-on-rails rubygems activeadmin
您向每个活动管理员资源添加对actions 的调用:
ActiveAdmin.register Foobar do
actions :all, :except => [:destroy]
end
【讨论】:
在某些时候我遇到了这个问题,因为销毁方法,“删除”按钮没有消失
actions :all, except: [:destroy]
controller do
def destroy # => Because of this the 'Delete' button was still there
@user = User.find_by_slug(params[:id])
super
end
end
【讨论】:
接受的答案引发了一个异常,“参数数量错误”,所以我这样做是为了排除删除按钮(:destroy action)
ActiveAdmin.register YourModel do
actions :index, :show, :new, :create, :update, :edit
index do
selectable_column
id_column
column :title
column :email
column :name
actions
end
【讨论】:
另一种从 ActiveAdmin 资源的 default_actions 中删除操作的方法是通过 config 变量,例如:
ActiveAdmin.register MyUser do
config.remove_action_item(:destroy)
...
end
通过
actions方法在接受的答案中已经提到了一种方法。
【讨论】:
如果您想完全删除删除销毁按钮,请使用:
actions :all, except: [:destroy]
但如果删除按钮需要基于资源属性的条件。(例如关联数据或状态)。
在索引页:
index do
# ...
actions defaults: false do |row|
if can? :read, row
text_node link_to "View", admin_resource_path(row), class: "view_link"
end
if can? :edit, row
text_node link_to "Edit", admin_resource_path(row), class: "edit_link"
end
if can? :destroy, row
text_node link_to I18n.t('active_admin.delete'), admin_resource_path(row), method: :delete, data: { confirm: I18n.t('active_admin.delete_confirmation') }, class: "delete_link" if row.deletable?
end
end
end
现在是复杂的部分,我不得不敲几次头才能在显示页面控制它:
config.remove_action_item(:destroy) # will remove the destroy button
action_item only: :show do
link_to I18n.t('active_admin.delete'), admin_resource_path(resource), method: :delete, data: { confirm: I18n.t('active_admin.delete_confirmation') }, class: "delete_link" if resource.deletable?
end
对不起我糟糕的格式。
【讨论】: