gpt4 book ai didi

ruby-on-rails - 如何在 aasm 事件上返回一个值?

转载 作者:行者123 更新时间:2023-12-04 06:04:04 24 4
gpt4 key购买 nike

如何使 aasm 事件返回 bool 值以外的值?我正在使用 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”方法来实现?

最佳答案

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

但是您可以包装状态转换并使用由 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

关于ruby-on-rails - 如何在 aasm 事件上返回一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4406504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com