Call Module function from Controller (NoMethodErro

2020-04-07 04:47发布

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条回答
Fickle 薄情
2楼-- · 2020-04-07 05:35

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
查看更多
登录 后发表回答