[英]Getting ArgumentError (wrong number of arguments) error but don't know which params are missing in Rails?
I know this is such a common error in Rails but I cannot figure out what params are missing. 我知道这在Rails中是一个常见的错误,但我无法弄清楚params缺失了什么。
I'm patching a fetch request from React to Rails to Update Sighting
which is a join table to Animal
and User
. 我正在修补从React到Rails的获取请求到Update Sighting
,它是Animal
和User
的连接表。 The sighting
model has a has_one_attached
using Active Storage. sighting
模型使用Active Storage进行has_one_attached
。 This is called image
and does not have to be an attribute of Sighting
table but from what I understand does need to be in strong_params. 这称为image
,不一定是Sighting
表的属性,但从我理解的确需要在strong_params中。
Here's the React fetch: 这是React fetch:
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 ]})
})
Here's the SightingController
: 这是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
the model Sighting
模特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
and ModelSerializer
: 和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
I was able to update a sighting via Rails console no problem with just updating the title
. 我能够通过Rails控制台更新目标,只需更新title
。
The Rails error: Rails错误:
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'
And here's an update run from console: 这是从控制台运行的更新:
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
I'll include the syntax error in your controller's params that was fixed by the commenters: 我将在您的控制器的参数中包含语法错误,该参数由评论者修复:
params.require(:sighting)...
Second of all, you need to pass your JSON with that sighting
param wrapping them up because it is required by your controller: 其次,你需要传递你的JSON和那个sighting
参数包装它们,因为你的控制器需要它:
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
}
})
That will give you the correct params when you PUT to your controller. 当您将PUT输入控制器时,这将为您提供正确的参数。 Otherwise, it'll not save your values because they will not pass your basic param validation. 否则,它将不会保存您的值,因为它们不会通过您的基本参数验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.