Reputation: 41
Gone through a lot of answers but still couldn't find the solution. I have been trying to get a successful response for a POST request to the following controller,
def create
@user = User.new(user_params)
if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
private
def user_params
params.require(:user).permit(:name, :email, :phone, :password)
end
end
Although I send all the parameters mentioned,in the request,I am still facing the error.
"status": 400,
"error": "Bad Request",
"exception": "#<ActionController::ParameterMissing: param is missing or the value is empty: user>"
I am using rails version 5.2.1
Upvotes: 1
Views: 4225
Reputation: 623
This Worked for me. I made just only one hash in params method instead of nested hash. my user_params method was:-
def user_params
params.permit(:name, :email, :phone, :password)
end
OR You can use your method but your parameter passed in user hash like below.
{user: {name: "bittu", email: "[email protected]", phone: 123456, password: "123456" } }
Upvotes: 0
Reputation: 1663
If you look at the logs, what does the data transferred in your post request look like?
When you get that type of error, often it's because you send the data like this:
{ first_name: 'John', last_name: 'Doe', ...}
When the server expects you to nest this into a user
object (hence the require(:user) in your strong_params
:
{ user: { first_name: 'John', ... } }
Upvotes: 1