简体   繁体   中英

As you can pass params from one action to another?

As you can pass params from one action to another?

#In this action the user passes the parameters of the credit card
def review
  @cart = current_cart

  ...
  ...
end

This action displays the sum of the order etc. And a Button that the user can confirm the order. If he will confirm, then the parameters must be passed in the model where the payment will be made.

def update
  @cart = current_cart
  @cart.update_attributes(params[:cart])

  Purchasing.purchase(current_user, current_cart, credit_card)
end


class Purchasing
  def initialize(user, cart, credit_card)
    @user = user
    @cart = cart
    @credit_card = credit_card
  end

  #This method of payment order.
  def purchase
    begin
      result = Braintree::Transaction.sale(
          amount: @cart.total,
          credit_card: {
            number: ??',
            cvv: ??,
            expiration_month: ??,
            expiration_year: ??
          }
      )

    ...
  end
end

try this

def update
  @cart = current_cart
  @cart.update_attributes(params[:cart])

  this_purchase = Purchasing.new (current_user, current_cart, credit_card)
  this_purchase.purchase
end

Ok, in params[] you will see the credit_card values. You will need to reference them individually instead of through params[:credit_card] . params[:name] , params[:cvv] , params[:expiration_month] , params[:expiration_year] are all available. Pass these individually or create a credit_card class to store them in to pass to the method.

Purchasing.new(current_user, current_cart, CreditCard.new(name: params[:name], cvv: params[:cvv], <and_so_on>)).purchase

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