ruby-on-rails - 出现 ArgumentError(错误数量的参数)错误,但不知道 Rails 中缺少哪些参数?
问题描述
我知道这是 Rails 中的一个常见错误,但我无法弄清楚缺少哪些参数。
我正在修补从 React 到 Rails 到 Update 的 fetch 请求,Sighting
它是一个连接表Animal
和User
. 该sighting
模型有一个has_one_attached
使用 Active Storage。这被称为image
并且不必是Sighting
表的属性,但据我了解确实需要在 strong_params 中。
这是 React 获取:
editSighting = (title, body, animalId, sightingId) => {
fetch(`http://localhost:9000/api/v1/sightings/${sightingId}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": localStorage.getItem("token")
},
body: JSON.stringify({
title: title,
body: body,
likes: this.state.likes,
animal_id: animalId,
user_id: this.state.currentUser.id
})
})
.then(r => r.json())
.then(newSighting => {
this.setState({ sightings: [...this.state.sightings, newSighting ]})
})
这是SightingController
:
class Api::V1::SightingsController < ApplicationController
before_action :find_sighting, only: [:update, :show, :destroy]
def index
@sightings = Sighting.all
render json: @sightings
end
def create
@sighting = Sighting.new(sighting_params)
@sighting.image.attach(params[:sighting][:image])
if @sighting.save && @sighting.image.attached
render json: @sighting, status: :accepted
else
render json: { errors: @sighting.errors.full_messages }, status: :unprocessible_entity
end
end
def update
# if curr_user.id == @sighting.user_id
@sighting.update(sighting_params)
if @sighting.save
render json: @sighting, status: :accepted
else
render json: { errors: @sighting.errors.full_messages }, status: :unprocessible_entity
end
end
def destroy
if curr_user.id == @sighting.user_id
@sighting.image.purge_later
@sighting.delete
render json: "sighting deleted"
else
render json: { errors: "You are not authorized to delete"}
end
end
private
def sighting_params
params.require[:sighting].permit(:title, :body, :likes, :image, :user_id, :animal_id)
end
def find_sighting
@sighting = Sighting.find(params[:id])
end
end
该模型Sighting
class Sighting < ApplicationRecord
has_one_attached :image
def image_filename
self.image.filename.to_s if self.image.attached?
end
def image_attached?
self.image.attached?
end
belongs_to :user
belongs_to :animal
has_many :comments, :as => :commentable, dependent: :destroy
end
和ModelSerializer
:
class SightingSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :id, :title, :body, :likes, :image, :created_at
belongs_to :animal
belongs_to :user
has_many :comments, :as => :commentable, dependent: :destroy
def image
rails_blob_path(object.image, only_path: true) if object.image.attached?
end
end
我能够通过 Rails 控制台更新目击,只需更新title
.
导轨错误:
Completed 500 Internal Server Error in 7ms (ActiveRecord: 6.2ms)
ArgumentError (wrong number of arguments (given 0, expected 1)):
app/controllers/api/v1/sightings_controller.rb:48:in `sighting_params'
app/controllers/api/v1/sightings_controller.rb:25:in `update'
这是从控制台运行的更新:
2.6.0 :017 > Sighting.first.update(title: "In NYC?? What a surprise!")
Sighting Load (0.6ms) SELECT "sightings".* FROM "sightings" ORDER BY "sightings"."id" ASC LIMIT $1 [["LIMIT", 1]]
(0.2ms) BEGIN
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 39], ["LIMIT", 1]]
Animal Load (0.4ms) SELECT "animals".* FROM "animals" WHERE "animals"."id" = $1 LIMIT $2 [["id", 231], ["LIMIT", 1]]
Sighting Update (0.6ms) UPDATE "sightings" SET "title" = $1, "updated_at" = $2 WHERE "sightings"."id" = $3 [["title", "In NYC?? What a surprise!"], ["updated_at", "2019-03-26 16:23:11.098248"], ["id", 7]]
(2.2ms) COMMIT
=> true
解决方案
我将在评论者修复的控制器参数中包含语法错误:
params.require(:sighting)...
其次,您需要传递带有sighting
包装它们的参数的 JSON,因为控制器需要它:
fetch(`http://localhost:9000/api/v1/sightings/${sightingId}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": localStorage.getItem("token")
},
body: JSON.stringify({
sighting: {
title: title,
body: body,
likes: this.state.likes,
animal_id: animalId,
user_id: this.state.currentUser.id
}
})
当您 PUT 到控制器时,这将为您提供正确的参数。否则,它不会保存您的值,因为它们不会通过您的基本参数验证。
推荐阅读
- git - 如何阻止 git push 悄悄地未能发布本地提交
- node.js - 代理与应用程序服务器的连接时如何使用 nginx 3rd 方模块
- c# - SQLiteDataReader 类型亲和力错误?
- python - 使用 glob 写入多个 csv
- php - 如何在 Cpanel 中使用 codeigniter 创建子域?
- java - Nez / Monogame : Can't click buttons but mouse actions are properly registering
- php - 如何将输入类型值传递给 php 中的另一个 php 文件
- android - Android 设备中的 Crystal 报告溢出-x 问题
- python - 如何在“while”循环中结束“try”循环?
- c++ - 在 C++ 中使用套接字 io 获取动态字节数