当我尝试使用GET /lecture_events/1
调用我的api时,我遇到了这个错误。错误为
ArgumentError (wrong number of arguments (given 2, expected 0..1)):
app/controllers/lecture_events_controller.rb:46:in `set_lecture_event'
这是我的选修课事件控制器代码
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
我一直在尝试调试有问题的函数,但在看完文档后,我很确定modelName.find()
就是这样调用的。我尝试传入一个字符串而不是params
,但它仍然抛出相同的错误。
有人知道它可能有什么问题吗?
我的rails版本是6.1.4
Ruby是3.0.2
,mongoid是7.0.5
编辑:这是我的LectureEvent
型号代码
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
以及完整的堆栈跟踪(如果需要(StackTrace
EDIT2:将"asd"
更改为正确的参数值
更新:
您的mongoid/i18n版本不支持ruby 3。
i18n表示,您需要使用ruby 3的关键字参数来调用I18n::translate
,但是mongoid使用散列来调用I18n::translate
。看起来这在最新的mongoid版本中已经修复,所以我建议升级到7.3.4。
注意:当发现LectureEvent
时,您没有看到此错误,因为在这种情况下不需要翻译任何错误。
我想我找到了答案。我刚刚检查了我的收藏,里面没有任何文档。所以我创建了一个伪文档,并使用该文档的id调用了api,它起了作用,但当我发送一个伪id时,它仍然给我带来了这个错误。
我现在唯一的问题是,ruby不应该告诉我没有找到有问题的文档,而不是这个论点错误吗?