简体   繁体   中英

How to render action from another action inside same controller and pass parameters

I am writing an application that needs to pass parameters or data to another action so that i break down the controller actions into manageable pieces. Whats the best way to do that? To test my code i was issuing a 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)

but its a GET request and i want a POST request and i read that it is not possible.

Thus is it possible to render another action and pass parameters in? Or is best if i make a model method to contain all the code in one place?

You could just call the other action like a function name from an action. For passing parameters you could just use instance variables.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM