Jack Pincus
Jack Pincus

Reputation: 61

ArgumentError in rails - calling method with arg unexpectedly

Using rails 5

I have the following controller action:

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

Which calls the following model method:

def send
    response = KairosService.enroll(self)
    self.update_attributes(response: response)
end

When I hit that controller action from the browser I get the following error:

 ArgumentError (wrong number of arguments (given 1, expected 0)):

app/models/enrollment.rb:1:in send'
app/controllers/enrollments_controller.rb:2:in create'

(line numbers in error are updated to reflect the snippets)

I can't figure out why the send action is getting called with the enrollment.new action? Why is this happening, and how do I prevent the action from getting called?

Upvotes: 1

Views: 151

Answers (1)

mahemoff
mahemoff

Reputation: 46369

Try renaming send to something else. You shouldn't create a method called send in Ruby as it's a standard (and important) method on Object (https://apidock.com/ruby/Object/send).

(Here Rails framework is trying to call the "real" send as part of object creation, but your code is overriding the definition.)

Upvotes: 2

Related Questions