将参数传递给aasm事件

时间:2017-10-15 05:39:08

标签: ruby-on-rails ruby state-machine aasm

我正在使用aasm statemachine。我有以下活动。这样做会将状态从order_created更改为payment_response_received。所以在更改之后我想调用一个方法verify_payment_response(data)。 我可以通过调用@booking.move_to_payment_response_received!来进行状态更改,但是如何传递参数以便在after回调中使用?

event :move_to_payment_response_received do
  after_commit do
         self.verify_payment_response(data) #How can I pass this data param from my controller
  end
  transitions from: :order_created, to: :payment_response_received
end

1 个答案:

答案 0 :(得分:3)

您可以在触发事件之前将data指定给局部变量:

# in your model

attr_accessor :payment_response_data

event :move_to_payment_response_received, :after_commit => :verify_payment_response do
  transitions from: :order_created, to: :payment_response_received
end

private

def verify_payment_response
  data = payment_response_data

  # already existing code to verify `data`
end

并在你的控制器中使用它:

@booking.payment_response_data = data
@booking.move_to_payment_response_received!