你可以将params从一个动作传递到另一个动作吗?

时间:2014-06-18 21:10:20

标签: ruby-on-rails ruby ruby-on-rails-4

你可以将params从一个动作传递到另一个动作吗?

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

  ...
  ...
end

此操作显示订单等的总和。用户可以确认订单的按钮。如果他将确认,则必须在将要付款的模型中传递参数。

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

2 个答案:

答案 0 :(得分:0)

试试这个

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

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

答案 1 :(得分:0)

好的,在params []中,您将看到credit_card值。您需要单独引用它们,而不是通过params[:credit_card]引用它们。 params[:name]params[:cvv]params[:expiration_month]params[:expiration_year]都可用。单独传递这些或创建一个credit_card类来存储它们以传递给方法。

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