Home > OS >  Ruby ArgumentError: wrong number of arguments (given 2, expected 0..1)
Ruby ArgumentError: wrong number of arguments (given 2, expected 0..1)

Time:02-24

I have been facing this error when I try to call my api using GET /lecture_events/1. The error is

ArgumentError (wrong number of arguments (given 2, expected 0..1)):
  
app/controllers/lecture_events_controller.rb:46:in `set_lecture_event'

and this is my lecture_events controller code

class LectureEventsController < ApplicationController
  before_action :set_lecture_event, only: [:show, :update, :destroy]

  # GET /lecture_events
  def index
    @lecture_events = LectureEvent.all

    render json: @lecture_events
  end

  # GET /lecture_events/1
  def show
    @lecture_event = LectureEvent.find(params[:id])
    render json: @lecture_event
  end

  # POST /lecture_events
  def create
    @lecture_event = LectureEvent.new(lecture_event_params)

    if @lecture_event.save
      render json: @lecture_event, status: :created, location: @lecture_event
    else
      render json: @lecture_event.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /lecture_events/1
  def update
    if @lecture_event.update(lecture_event_params)
      render json: @lecture_event
    else
      render json: @lecture_event.errors, status: :unprocessable_entity
    end
  end

  # DELETE /lecture_events/1
  def destroy
    @lecture_event = LectureEvent.find(params[:userID])
    @lecture_event.destroy
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_lecture_event
      @lecture_event = LectureEvent.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def lecture_event_params
      params.require(:lecture_event).permit(:timestamp, :eventType, :lectureID, :userID)
    end
end

I have been trying to debug the function in question but after going through the docs I'm pretty sure thats how modelName.find() is called. I tried passing in a string instead of params but it still throws the same error.

Does anybody know what could be the problem with it ?

My rails version is 6.1.4 Ruby is 3.0.2 and mongoid is 7.0.5

EDIT: Here is my LectureEvent model code

class LectureEvent
  include Mongoid::Document
  field :timestamp, type: Date
  field :event_type, type: String
  field :lecture_id, type: String
  field :user_id, type: String
end

and the full stack trace if it's needed StackTrace

EDIT2: Changed "asd" to the correct parameter value

CodePudding user response:

I think I found the answer. I just checked my collection and there weren't any documents in it. So I created a dummy document and called the api using that document's id and it worked but the moment I sent in a dummy id it still threw me this error.

My only question now is shouldn't ruby just inform me that the document in question wasn't found and not this argument error?

CodePudding user response:

Update:

Your version of mongoid/i18n doesn't support ruby 3.

i18n says you need to call I18n::translate with keyword arguments for ruby 3, but mongoid is calling I18n::translate with a hash. It looks like this is fixed in the latest mongoid version, so I suggest upgrading to 7.3.4.

Note: you didn't see this error when a LectureEvent was found because no error needs to be translated in that case.

  • Related