Bee*_*tty 11 ruby ruby-on-rails ruby-on-rails-3.2
我在ApplicationController中定义了方法
class ApplicationController < ActionController::Base
helper_method :get_active_gateway
def get_active_gateway(cart)
cart.account.gateways
end
end
Run Code Online (Sandbox Code Playgroud)
当我在模型中调用此方法时
class Order < ActiveRecord::Base
def transfer
active= get_active_gateway(self.cart)
end
end
Run Code Online (Sandbox Code Playgroud)
它抛出错误undefined local variable get_active_gateway.
所以我写了
class Order < ActiveRecord::Base
def transfer
active= ApplicationContoller.helpers.get_active_gateway(self.cart)
end
end
Run Code Online (Sandbox Code Playgroud)
然后就是扔了error undefined method nil for Nilclass.
我在Rails 3.2.0中工作.
作为设计选择,不建议您从模型中调用控制器助手.
您只需将所需的详细信息作为参数传递给模型方法即可.
def transfer(active_gateway) active = active_gateway end