RegistrationInsert:Class 的未定义方法“插入”

undefined method `insert' for RegistrationInsert:Class

我试图从我的控制器调用我在 registration_insert.rb 中定义的方法,但我不断收到以下错误:

undefined method `insert' for RegistrationInsert:Class

如何正确调用插入方法?

文件结构(都在/app下)

-控制器

----------registrations_controller.rb

-注册人数

----------registration_insert.rb

registrations_controller.rb:

 def create
    @registration = Registration.new(registration_params)
    RegistrationInsert.insert(registration_params)


    respond_to do |format|
      if @registration.save
        format.html { redirect_to @registration, notice: 'Registration was successfully created.' }
        format.json { render :show, status: :created, location: @registration }
      else
        format.html { render :new }
        format.json { render json: @registration.errors, status: :unprocessable_entity }
      end
    end
  end

registration_insert.rb

module RegistrationInsert 
    def insert(registration)
        #method code
    end
end

编辑:将 class 更改为模块,它给我这个错误:

undefined method `insert' for RegistrationInsert:Module

也许您只需要 RegistrationInsert.new.insert

P.S。请考虑将 RegistrationInsert 设为 class,而不是模块。 类 更适合服务对象实现。

P.P.S。如果要RegistrationInsert.new,则在方法前加上self.

module RegistrationInsert 
  def self.insert(registration)
    #method code
  end
end