第一次使用 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未定義。
uj5u.com熱心網友回復:
Amember_action是一個控制器動作,這將生成一個路由。你永遠不應該打電話member_action給另一個人。
Approach1 :- 您可以包含模塊并在那里添加方法。
一世。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
ii. 添加檔案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
有關更多詳細資訊,您可以在此處參考檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480485.html
