从控制器调用模块函数(NoMethodError) [英] Call Module function from Controller (NoMethodError)

查看:23
本文介绍了从控制器调用模块函数(NoMethodError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个模块MiddleMan"我可以在 rails 控制台中很好地调用它,但在控制器中我得到一个 NoMethodError

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

在控制器中我有:

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

在 middleman.rb 模块中,我有:

And in the middleman.rb module I have:

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

有什么想法吗?

推荐答案

因为你包含了模块,实例方法 read_catalog 被添加到你的类中,所以你可以直接调用它:

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

这篇关于从控制器调用模块函数(NoMethodError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆