0

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

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

如何正确调用插入方法?

文件结构(全部在 /app 下)

-控制器

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

-注册

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

注册控制器.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

注册插入.rb

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

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

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

4

1 回答 1

0

也许你只需要RegistrationInsert.new.insert

PS请考虑做RegistrationInsert一个类,而不是模块。类更适合服务对象的实现。

PPS 如果需要RegistrationInsert.new,则self.在方法之前添加:

module RegistrationInsert 
  def self.insert(registration)
    #method code
  end
end
于 2015-06-29T04:44:28.400 回答