使用导轨 5
我有以下控制器操作:
def create
enrollment = current_user.enrollments.new(image: enroll_params[:image])
enrollment.send
if enrollment.save
flash[:notice] = ['It looks like it worked']
flash.keep(:notice)
render js: "window.location = '#{root_path}'"
end
end
它调用以下模型方法:
def send
response = KairosService.enroll(self)
self.update_attributes(response: response)
end
当我从浏览器点击该控制器操作时,出现以下错误:
ArgumentError (wrong number of arguments (given 1, expected 0)):
app/models/enrollment.rb:1:in send'
app/controllers/enrollments_controller.rb:2:in create'
(错误的行号将更新以反映代码段(
我不明白为什么发送操作会随着 enrollment.new 操作而调用?为什么会发生这种情况,如何防止调用该操作?
尝试将send
重命名为其他名称。您不应该在 Ruby 中创建一个名为send
的方法,因为它是 Object (https://apidock.com/ruby/Object/send( 上的标准(且重要(方法。
(在这里,Rails框架试图调用"真实"发送作为对象创建的一部分,但你的代码覆盖了定义。