2016-08-18 25 views
0

我正在編寫一個需要將參數或數據傳遞給另一個動作的應用程序,以便將控制器操作分解爲可管理的部分。什麼是最好的方式來做到這一點?爲了測試我的代碼,我發出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請求,我讀了這是不可能的。

因此,是否有可能呈現另一個動作並傳遞參數?或者,如果我使用一種模型方法在一個地方包含所有代碼,最好?

回答

0

你可以從動作中調用其他動作,如函數名稱。 爲了傳遞參數,你可以使用實例變量。

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 
+0

嘿,如果我不想呈現任何東西,這仍然工作? –

+0

確定如果你跳過渲染動作部分它只是完成了什麼action_one做了 – neongrau

+0

現在,我認爲它應該不需要定義@params,因爲無論如何,action_one中的原始參數應該可以被action_two訪問。 – neongrau

相關問題