ActionController::ParameterMissing(参数缺失或值为空:避难所):

ActionController::ParameterMissing (param is missing or the value is empty: shelter):

我正在向邮递员发出请求,但我的避难所参数一直为空,即使我已将其格式化为让避难所参数正确包含表单数据。如果我遗漏了什么,请帮助我。

class Api::V1::SheltersController < ApplicationController

  def index
    @shelters = Shelter.all.order(:id).reverse
    respond_with @shelters
  end

  def show
    @shelter = Shelter.find(params[:id])
    respond_with @shelter
  end

  def create
    @shelter = Shelter.new(shelter_params)
    if @shelter.save
      render json: @shelter, status: 201
    else
      render json: { errors: @shelter.errors.full_messages }, status: 422
    end
  end


  private

    def shelter_params
      params.require(:shelter).permit(:name, :address, :phone, :beds)
    end

end

我通过 Postman 发送的数据

shelter : {
    name : "Pee's Shelter",
    address : "Pee's Address",
    phone : "(236)817-4853",
    beds : 5
} 

我在 rails 页面上看到的请求信息

参数:

{"shelter : {\n\t\tname : \"小便避难所\",\n\t\taddress : \"Pee's Address\",\n\t\tphone : \"(236)817-4853\",\n\t\tbeds : 5\n} \n"=>无, "subdomain"=>"api", "format"=>:json}

您的密钥设置为:

"shelter : {\n\t\tname : \"Pee's Shelter\",\n\t\taddress : \"Pee's Address\",\n\t\tphone : \"(236)817-4853\",\n\t\tbeds : 5\n} \n"

你的键值为 => nil

删除字符串前的"

更准确地说:不要将你的避难所和所有钥匙都创建为一根绳子。