[英]Rails: How to pass value from one action to another in the same controller
[英]How to render action from another action inside same controller and pass parameters
我正在編寫一個需要將參數或數據傳遞給另一個動作的應用程序,以便將控制器動作分解為可管理的部分。 最好的方法是什么? 為了測試我的代碼,我發出了redirect_to
new_calendar_account = {
calendar_account: {
provider: "google",
access_token: auth_client.access_token,
refresh_token: auth_client.refresh_token,
token_expires_at: token_expiry
}
}
redirect_to new_calendar_account_path(new_calendar_account)
但它是一個GET請求,我想要一個POST請求,我讀到這是不可能的。
因此,可以渲染另一個動作並傳遞參數嗎? 還是最好的方法是將模型中的所有代碼都放在一個地方?
您可以只調用另一個動作,例如從一個動作中調用函數名稱。 對於傳遞參數,您可以只使用實例變量。
def action_one
@params = params
action_two # to execute the code from action_two
render action: :action_two and return # to render view of action_two
end
def action_two
@params ||= params
# work with passed @params
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.