如何在 rails 活动管理员中使用参数调用另一个操作?

How to call another action with parameter in rails active admin?

第一次使用rails,一切都很陌生,请多多包涵。

我的代码是这样的:

ActiveAdmin.register Post do

  member_action :action1 do
    <some code>
  end

  member_action :action2 do
    if(<some check>)
      <some work>
    else
      <here I need to call the action1 with parms action2 has>
    end
  end

end

我应该怎么做?如何使用 else 块中的参数调用 action1?

我试过了

redirect_to action1(request.parameters)

它没有工作,它抛出错误 action1 is undefined。

A member_action 是一个控制器动作,这将生成一条路线。 你不应该从另一个调用 member_action

方法 1:- 您可以包含模块并在其中添加方法。

我。 include 模块

include ActiveAdminHelper
ActiveAdmin.register Post do

  member_action :action1 do
    method1
  end

  member_action :action2 do
    if(<some check>)
      method1
    else
      method2
  end
end

二。添加文件 app/helpers/active_admin_helper.rb

module ActiveAdminHelper
  def method1
  end

  def method2
  end
end

Approach2:- 修改控制器 -> 您可以在控制器块中定义方法

  ActiveAdmin.register Post do

    controller do
      # This code is evaluated within the controller class

      def define_a_method
        # Instance method
      end
    end
  end

更多细节你可以参考文档here