ruby-on-rails – 如何在模型中调用ApplicationController中定义
发布时间:2020-12-16 22:33:54 所属栏目:百科 来源:网络整理
导读:我已经在ApplicationController中定义了方法 class ApplicationController ActionController::Base helper_method :get_active_gateway def get_active_gateway(cart) cart.account.gateways endend 当我在模型中调用此方法时 class Order ActiveRecord::Bas
我已经在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局部变量get_active_gateway. 所以我写了 class Order < ActiveRecord::Base def transfer active= ApplicationContoller.helpers.get_active_gateway(self.cart) end end 那么Nilclass就是抛出错误未定义的方法. 我在Rails 3.2.0中工作. 请给出解决方案. 解决方法
为什么你需要这样的东西?该模型不应该知道它的控制器.也许在这种情况下,您的系统的重新设计将更为合适.
这里是一个链接到类似的thread. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |