未显示活动管理员操作项

active admin action item not showing

I am following active_admin's documentation on action items 试图将 "approve" 操作添加到我的管理视图。

我的 activeadmin 注册设置如下:

ActiveAdmin.register PendingClaim do


  action_item :approve, method: :post, only: [:show, :index] do
    link_to('Approve', "#")
  end


  index do
    column "Business ID", :business_id
    column "User ID", :user_id
    column "Claim approved by Admin?", :approved
    column :created_at
  end



  controller do
    # This code is evaluated within the controller class

    def approve
      binding.pry
    end
  end
end

但它没有在 table 中显示批准操作。我希望批准操作映射到 PendingClaim 控制器中的#approve 操作。不确定我必须在这里做什么...

我也试过像这样向我的索引添加操作:

  index do
    column "Business ID", :business_id
    column "User ID", :user_id
    column "Claim approved by Admin?", :approved
    column :created_at
    actions
  end

但这只是显示了默认操作,而没有我的自定义批准操作

编辑 --

根据@Omnigazer 的回答,我将代码更改为

ActiveAdmin.register PendingClaim do


  member_action :approve, only: :index do
    redirect_to resource_path, notice: "Approved!"
  end


  index do
    column "Business ID", :business_id
    column "User ID", :user_id
    column "Claim approved by Admin?", :approved
    column :created_at
  end



  controller do
    # This code is evaluated within the controller class

    def approve
      binding.pry
    end
  end
end

但仍然没有显示动作。

编辑 --

多亏了 Omnigazer,我才让它工作,我的代码:

ActiveAdmin.register PendingClaim do
  member_action :approve, method: :post, only: :index do
  end

  index do
    column :created_at
    column 'Business ID', :business_id
    column 'User ID', :user_id
    column 'Claim approved by Admin?', :approved
    actions defaults: false do |pending_claim|
      params = { business_id: pending_claim.business_id,
                 user_id: pending_claim.user_id }
      link_to('Approve', approve_admin_pending_claim_path(pending_claim, params), method: :post)
    end
  end

  controller do
    # This code is evaluated within the controller class

    def approve
      business = Business.find(params[:business_id])
      user = Business.find(params[:user_id])
      business.user_id = user.id
      business.verified = true
      if business.save
        resource.approved = true
        resource.save
        redirect_to resource_path(resource), notice: 'Claim Approved!'
      end
    end
  end
end

操作项显示在页面的右上角,通常是 "create %{model_name%}" 按钮所在的位置。尝试在那里寻找它。否则,您的代码看起来有效。虽然 ActiveAdmin 有自己的 DSL 方法 "collection_action" 和 "member_action" 来处理像你这样的情况。尝试在文档中查找它们。

编辑: 如果你想在默认操作旁边附加一个操作项,试试这个:

index do
  ...
  actions defaults: true do |order|
    link_to('Approve', "#")
  end
end
#action_item(name = nil, options = {}, &block) ⇒ Object

Add a new action item to the resource

Parameters: name (Symbol) (defaults to: nil) options (Hash) (defaults to: {}) — valid keys include: :only: A single or array of controller actions to display this action item on. :except: A single or array of controller actions not to

display this action item on.

action_item documentaion

我认为那里没有选项method:

active admin 文档有很好的使用示例

page_action :add_event, method: :post do
  # ...
  redirect_to admin_calendar_path, notice: "Your event was added"
end

action_item :add do
  link_to "Add Event", admin_calendar_add_event_path, method: :post
end

source