有没有办法检查从模型中调用了哪个 Controller 方法?
例子: 假设调用了 Controller 创建方法:
def create
do something
end
然后在模型中只在调用 Controller 中的创建时才做一些事情
if create?
do something
end
最佳答案
我想您可以检查调用堆栈,但这正是模型不的目的:它们现在不应该与 Controller 有关。
检查堆栈:
if caller.grep /create/
# do something
elsif caller.grep /update/
#do something else
end
应该可以解决问题。
关于ruby-on-rails - rails : checking which controller method was called from within the model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2925852/