如何使aasm事件返回布尔值以外的值?我使用的是aasm 2.2.0
例如,有一个MusicPlayer模型,它在启动时随机播放一首歌曲
aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
:transitions :from => :stopped, :to => :started
end
def play_song
# select and play a song randomly and return the song object
end现在,如果我想在启动播放器时返回当前正在播放的歌曲,我该如何通过'play_song‘方法来实现呢?
发布于 2012-10-20 16:36:32
你不能这么做。返回状态用于指示转换是否成功。但是我很好奇你有什么用例需要它。
但是您可以包装状态转换并使用由play_song方法设置的变量,如下所示:
aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
:transitions :from => :stopped, :to => :started
end
def play_song
# select and play a song randomly
@song = ...
end
def shuffle
if start
@song
end
endhttps://stackoverflow.com/questions/4406504
复制相似问题