下面是我的示例代码:
class Foo < ActiveRecord::Base
include AASM
aasm_column :status
aasm_initial_state :start_state
aasm_state :start_state
aasm_state :state_two
aasm_state :end_state
aasm_event :move_to_two, :guard => :guard_callback, :after => :after_callback do
transitions :from => :start_state, :to => :state_two
end
def guard_callback
puts "executing guard callback..."
false
end
def after_callback
puts "executing after callback..."
end这是我的代码外观的一个玩具表示。我只是从guard回调中返回false,以测试不执行转换或后续转换的行为。下面是我在测试中调用的代码
foo = Foo.new
foo.move_to_two!
puts "foo's current status: #{foo.status}"下面是输出
executing after callback...
foo's current status: state_two注意守卫从来不会被叫到..。
我把守卫放错地方了吗?我是不是错了,返回false会停止转换?停止转换是否也会导致忽略after回调?或者,无论发生什么,它都会执行after?
如果最后一件事是真的,我如何将状态传递到回调中?
提前感谢,如果您需要更多信息,请让我知道...
jd
发布于 2011-10-27 20:07:00
好的,我弄明白了(整个“只要你问,你就会找到答案”的事情)...the :guard就像这样进行转换:
aasm_event :move_to_two, :after => :after_callback do
transitions :from => :start_state, :to => :state_two, :guard => :guard_callback
endhttps://stackoverflow.com/questions/7915408
复制相似问题