Controller Concern 中的 ClassMethods 返回 NoMethodError

ClassMethods in Controller Concern returning NoMethodError

我在 controllers/concerns 中创建了一个名为 reports.rb

的问题

我在其中创建了 ClassMethods 模块。它看起来像这样

module Reports
  extend ActiveSupport::Concern

included do
    require 'peddler'
    require 'csv'
end

module ClassMethods

    def report_details(token, marketplace_id, merchant_id)
      #...
    end

我在我的商家控制器中包含了这个问题,如下所示:

class MerchantsController < ApplicationController
  include Reports

然后尝试在 Merchants 控制器的操作中调用 report_details 方法,如下所示:

def pull_from_amazon
    me = Merchant.find(params[:merchant_id])
    marketplace = me.marketplace
    token = me.token
    Merchant.report_details(token, marketplace, params[:merchant_id])
    redirect_to root_path
end

根据这个 post Rich on Rails 我应该可以调用:

Merchant.report_details(xx,xxx,xxxx)

但是当我尝试时出现 NoMethodError.. 我也试过:

me.report_details(xx,xxx,xxxx)

并得到相同的 NoMethodError

我做错了什么?

试试这个

MerchantController.report_details(xx,xxx,xxxx)

但是你最好在模型中包含这个问题。