如何在模型中调用 ApplicationController 中定义的方法 [英] How to call methods defined in ApplicationController in models
本文介绍了如何在模型中调用 ApplicationController 中定义的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 ApplicationController 中定义了方法
I have defined method in ApplicationController
class ApplicationController < ActionController::Base
helper_method :get_active_gateway
def get_active_gateway(cart)
cart.account.gateways
end
end
当我在模型中调用这个方法时
When I am calling this method in model
class Order < ActiveRecord::Base
def transfer
active= get_active_gateway(self.cart)
end
end
它抛出错误未定义的局部变量get_active_gateway
.
所以我写了
class Order < ActiveRecord::Base
def transfer
active= ApplicationContoller.helpers.get_active_gateway(self.cart)
end
end
然后它抛出error undefined method nil for Nilclass
.
我在 Rails 3.2.0 中工作.
I am working in Rails 3.2.0.
推荐答案
你为什么需要这样的东西?模型不应该知道它的控制器.在这种情况下,重新设计系统可能更合适.
Why would you need such thing? The model should not know about its controllers. Maybe a redesign of your system will be more appropriate in this case.
这里是类似线程的链接.
这篇关于如何在模型中调用 ApplicationController 中定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文