我在ApplicationController中定义了方法
class ApplicationController < ActionController::Base
helper_method :get_active_gateway
def get_active_gateway(cart)
cart.account.gateways
end
end
当我在模型中调用这个方法时
class Order < ActiveRecord::Base
def transfer
active= get_active_gateway(self.cart)
end
end
它抛出错误 undefined local variable 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。
最佳答案
为什么需要这样的东西?该模型不应该知道它的 Controller 。在这种情况下,重新设计系统可能更合适。
这是类似 thread 的链接.
关于ruby-on-rails - 如何在模型中调用 ApplicationController 中定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10190995/