活动管理栏中的代码可重用性问题

问题描述 投票:0回答:2

如何避免在活动管理操作项和控制器操作中使用的公共代码。我见过人们曾经在控制器和主动管理操作项中编写相同的代码。有什么办法可以避免吗?

示例就像我想要取消用户一样,它需要3-4个步骤才能完成。所以我在用户控制器中写了这个代码。

现在我有资源用户在活动管理员,我想从活动管理员删除用户。我创建了一个操作项,并再次编写了与成员操作中删除用户相同的代码。

有没有办法避免上述。

ruby-on-rails activeadmin
2个回答
0
投票

如果你想在不同的控制器之间共享代码,你应该关注轨道问题https://api.rubyonrails.org/v5.2.2/classes/ActiveSupport/Concern.html


0
投票

假设这是在User寄存器块(可能在app/admin/users.rb),这可能会帮助你。 action_item只包含实际member_action的链接(实际上是一个发布的表单)。这只是示例代码:

action_item :cancel_user, :only => :edit do
 link_to 'Cancel user',  do_cancel_user_admin_user_path(resource),   method => :post
end

member_action :do_cancel_user, :method => :post do
  flash.notice = "User will be canceled"
  resource.cancel # I guess this would the 4 lines of code that you are repeating
  redirect_to edit_admin_user_path(resource) and return
end

如果这确实/没有让您感到困惑,请告诉我。祝好运!

附:几个星期前有人问了类似的东西,这也可能有所帮助:How to reset user´s password by Devise authentication token directly from edit_page in Active Admin?

© www.soinside.com 2019 - 2024. All rights reserved.