ruby-on-rails ActionController::ParameterMissing(缺少参数或值为空:名称)

ldioqlga  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(154)

由于某种原因,我无法通过Postman成功地进行POST或UPDATE。我得到的错误如下:

Started POST "/names" for ::1 at 2022-10-19 16:26:48 -0500
Processing by NamesController#create as */*
  Parameters: {"_json"=>[{"name"=>"Joseph Schmoseph"}], "name"=>{}}
Completed 400 Bad Request in 0ms (ActiveRecord: 1.2ms | Allocations: 255)

  
ActionController::ParameterMissing (param is missing or the value is empty: name):
  
app/controllers/names_controller.rb:57:in `names_params'
app/controllers/names_controller.rb:19:in `create'

下面是我的names_controller及其所有路由:

class NamesController < ApplicationController
  before_action :set_name, only: [:show, :update, :destroy]

  # GET /names
  def index
    @names = Name.all

    render json: @names
  end

  # GET /names/1
  def show
    render json: @name
  end

  # POST /names
  def create
    @name = Name.new(name_params)

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

  # PATCH/PUT /names/1
  def update
    if @name.update(name_params)
      render json: @name
    else
      render json: @name.errors, status: :unprocessable_entity
    end
  end

  # DELETE /names/1
  def destroy
    @name.destroy
  end

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

    # Only allow a list of trusted parameters through.
    def name_params
      params.require(:name).permit(:name)
    end
end

除了UPDATE和CREATE之外,所有其他的路由都运行良好。我使用Postgresql作为我的数据库。我以前从未遇到过简单数据库路由的问题,所以我有点困惑。任何帮助都将不胜感激!

ulmd4ohb

ulmd4ohb1#

@Maxence表示
在 Postman
变更

{
    name: "Joseph Schmoseph"
  }

{
   name: {
      name: "Joseph Schmoseph"
   }
 }

在名称_控制器.rb中

// Parameters: {"name"=>{"name"=>"Joseph Schmoseph"}, "commit"=>"Create"} 
  
 // fixed this issue: ActionController::ParameterMissing (param is missing or the value is empty: name):
  
  def create 
    ....
  end

相关问题