简体   繁体   English

未初始化的常量 Admin::ModeratorsController::ModeratorInteractor

[英]uninitialized constant Admin::ModeratorsController::ModeratorInteractor

Hi i am working on a RoR project with ruby-2.3.0 and rails 4. I am trying to call a method of interactor from controller.嗨,我正在使用 ruby-2.3.0 和 rails 4 进行 RoR 项目。我正在尝试从 controller 调用交互器方法。 My controller is inside the Admin directory as follows:我的 controller 位于 Admin 目录中,如下所示:

class Admin::ModeratorsController < Admin::ApplicationController
  include Interactor

  def index
    ModeratorInteractor.find_abc(params)
  end
end

My interactor is:-我的互动者是:-

# frozen_string_literal: true

class ModeratorInteractor
  def self.find_abc(params)
    User.all
  end
end

When i run my code i got an error uninitialized constant Admin::ModeratorsController::ModeratorInteractor .当我运行我的代码时,我得到了一个错误uninitialized constant Admin::ModeratorsController::ModeratorInteractor I also try to include the Interactor:-我还尝试包括交互器:-

include Interactor

Please help how to fix it.Thanks in advance.请帮助如何解决它。在此先感谢。

You need to define ModeratorInteractor as module to include it in your controller: 您需要将ModeratorInteractor定义为module以将其包括在控制器中:

module ModeratorInteractor
  def self.find_abc(params)
    User.all
  end
end

Then you need to ensure that the module is loaded properly: 然后,您需要确保模块已正确加载:

# in application.rb
config.autoload_paths += %W("#{config.root}/lib") # path to your module

Or you can also use require instead of autoload_paths : 或者,您也可以使用require代替autoload_paths

require "#{Rails.root}/lib/modeator_interactor"

Then in your controller, you can include it: 然后在您的控制器中,可以包含它:

include ModeratorInteractor

First, you need to include Interactor in your ModeratorInteractor , also you need to define a call method, not find_abc which will not work and it will throw and error of undefined method , so your final interactor will look like this首先,您需要在您的ModeratorInteractorinclude Interactor ,还需要定义一个call方法,而不是find_abc ,它将不起作用,它会抛出undefined method的错误,所以您的最终交互器将如下所示

# frozen_string_literal: true

class ModeratorInteractor
  include Interactor

  def self.call
    params = context.params
  end
end

and you will call it as你会称它为

ModeratorInteractor.call(params: params)

Voila.瞧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM