简体   繁体   中英

Should I use Net::HTTP when I want to make POST request to an action

I have an action create and another action register_and_create . The first creates subscription and the seconds creates user and subscription. But I will have to copy the code of create action in register_and_create action. So is it good practice to use Net::HTTP to send POST request from register_and_create to create action?

No, it is not a good practice.

If it was GET method, you should execute action code and return redirection to the second action.

For POST methods you should execute all code in register_and_create action. Sending NET:HTTP might cause a lot of problems - you have to make sure you assigned all HTTP headers, unnecessary requests comes to the server, etc.

If you found common parts of codes it might be good to move your code to lib module and use include in your controllers.

For example:

module RegistrationModule
  def create_user
    # your code goes here
  end
end

class RegistrationController < ApplicationController
  include RegistrationModule

  def register_and_create
    # subscribe
    create_user
  end

  def create
    create_user
  end
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