如何在模型中调用ApplicationController中定义的方法

我在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抛出error undefined method nil for Nilclass

我在Rails 3.2.0中工作。

你为什么需要这样的东西? 模型不应该知道它的控制器。 在这种情况下,重新设计系统可能更合适。

这是类似线程的链接。

作为设计选择,不建议您从模型中调用控制器助手。

您只需将所需的详细信息作为参数传递给模型方法即可。


 def转移(active_gateway)
   active = active_gateway
结束