Call Module function from Controller (NoMethodError)

我的梦境 提交于 2019-12-30 22:29:12

问题


So I have a module "MiddleMan" I am able to call it just fine in the rails console but in the controller I am getting a NoMethodError

In the controller I have:

class SignUpController < ApplicationController
  include MiddleMan
  def page_one
      @package = MiddleMan::read_catalog("a", "b", "c")
  end
end

And in the middleman.rb module I have:

module MiddleMan
  def read_catalog(package, payment, coupon)
    Package.new(:price => "4.99")
  end
end

Any thoughts?


回答1:


Since you included the module the instance method read_catalog is added to your Class, so you can call it directly:

class SignUpController < ApplicationController
  include MiddleMan
  def page_one
      @package = read_catalog("a", "b", "c")
  end
end


来源:https://stackoverflow.com/questions/6604272/call-module-function-from-controller-nomethoderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!