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

Time:05-23

First time using rails, everything seems alien, please be kind with this stupid question.

I code something like:

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

How should I do this? How can I call action1 with the parms in the else block?

I tried

redirect_to action1(request.parameters)

It didn't work it throw error action1 is undefined.

CodePudding user response:

A member_action is a controller action and this will generate a route. You should never call the one member_action from another.

Approach1:- You can include the modules and add the method there.

i. include the module

include ActiveAdminHelper
ActiveAdmin.register Post do

  member_action :action1 do
    method1
  end

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

ii. Add the file app/helpers/active_admin_helper.rb

module ActiveAdminHelper
  def method1
  end

  def method2
  end
end

Approach2:- Modifying the Controller -> You can define the methods in the controller block

  ActiveAdmin.register Post do

    controller do
      # This code is evaluated within the controller class

      def define_a_method
        # Instance method
      end
    end
  end

For more details you can refer the doc here

  • Related