问题描述
是否可以在转换中发送变量?即
Is it possible to send variables in the the transition? i.e.
@car.crash!(:crashed_by => current_user)
我的模型中有回调,但我需要将发起转换的用户发送给他们
I have callbacks in my model but I need to send them the user who instigated the transition
after_crash do |car, transition|
# Log the car crashers name
end
我无法访问 current_user 因为我在模型中而不是控制器/视图中.
I can't access current_user because I'm in the Model and not the Controller/View.
在你说出来之前......我知道我知道.
And before you say it... I know I know.
不要尝试访问模型中的会话变量
我明白了.
然而,每当您希望创建一个回调来记录或审核某些内容时,您很可能想知道是谁引起的?通常我会在我的控制器中做一些类似的事情......
However, whenever you wish to create a callback that logs or audits something then it's quite likely you're going to want to know who caused it? Ordinarily I'd have something in my controller that did something like...
@foo.some_method(current_user)
我的 Foo 模型会期望一些用户发起 some_method 但我如何通过 StateMachine gem 的转换来做到这一点?
and my Foo model would be expecting some user to instigate some_method but how do I do this with a transition with the StateMachine gem?
推荐答案
如果您指的是 state_machine gem - https://github.com/pluginaweek/state_machine - 然后它支持事件的参数
If you are referring to the state_machine gem - https://github.com/pluginaweek/state_machine - then it supports arguments to events
after_crash do |car, transition|
Log.crash(car: car, crashed_by: transition.args.first)
end
这篇关于将变量传递给 Rails StateMachine gem 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!