如何在模型中调用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 结束
- nil的未定义方法`eq’:nilClass with rails 3和ruby enterprise on ubuntu hardy
- 这对于sinatra + angular.js +自定义提供程序的Oauth2工作流的概念是否正确?
- 如何从gemspec文件中获取版本?
- Nils和方法链接
- 从String#split返回零长度字符串
- postgresql datababse出错:服务器是否在本地运行并接受Unix域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接?
- 在Rails中保存和检索数组
- 无法使用电子表格gem(Ruby)设置Excel样式
- 如何在rails 4中使用params.require