For some reason I can't get a POST or UPDATE to successfully work via Postman. The error I get is as follows:
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'
Here is my names_controller with all routes:
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
All other routes are working fine execpt the UPDATE and CREATE. I'm using Postgresql as my db. I've never had this issue before with simple db routes so I'm a bit stumped. Any help would be highly appreciated!
CodePudding user response:
@Maxence means
In postman
change
{
name: "Joseph Schmoseph"
}
to
{
name: {
name: "Joseph Schmoseph"
}
}
In names_controller.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