是否可以在转换中发送变量? IE。
@car.crash!(:crashed_by => current_user)
我的模型中有回调,但我需要将发起转换的用户发送给他们
after_crash do |car, transition|
# Log the car crashers name
end
我无法访问 current_user,因为我在模型中而不是 Controller / View 中。
在你说之前...我知道我知道。
不要尝试访问模型中的 session 变量
我得到它。
但是,每当您希望创建一个记录或审核某些内容的回调时,您很可能想知道是谁造成的?通常情况下,我的 Controller 中会有一些类似...
@foo.some_method(current_user)
并且我的 Foo 模型会期望一些用户来煽动 some_method 但是我如何通过 StateMachine gem 的转换来做到这一点?
最佳答案
如果您指的是 state_machine gem - https://github.com/pluginaweek/state_machine - 然后它支持事件的参数
after_crash do |car, transition|
Log.crash(car: car, crashed_by: transition.args.first)
end
关于ruby-on-rails - 将变量传递给 Rails StateMachine gem 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3520379/