3

如何使 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”方法呢?

4

1 回答 1

0

你不能那样做。返回状态用于指示转换是否有问题。但我很好奇你有什么用例需要它。

但是您可以包装状态转换并使用该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
end
于 2012-10-20T08:36:32.163 回答