I want to use create a user account using postman, rails 5, and the devise gem. I added a username field to the devise user model and confirmed that it is working fine. However, when I send a post request to the Rails API, it is not creating the user as expected. Here is how the process goes through:
I'm sending this JSON post request to the URL: http://0.0.0.0:3000/users/
{
"username": "username",
"email": "[email protected]",
"password": "password",
"password_confirmation": "password"
}
Upon sending this request, I get the following response:
{
"status": "ERROR",
"message": "Could not create new user account",
"data": {
"email": [
"can't be blank"
],
"password": [
"can't be blank"
]
}
}
I created another Registrations Controller which inherits from Devise::RegistrationController. Here it is:
1 module Api
2 module V1
3 class RegistrationsController < Devise::RegistrationsController
4
5 before_action :configure_permitted_parameters
6 respond_to :json
7 def new
8 user = User.new(user_params)
9 end
10
11 def create
12 user = User.create(username: params[:username],
13 email: params[:email],
14 password: params[:password],
15 password_confirmation: params[:password_confirmation])
16
17 if user.save
18 render json: {status: "SUCCESS", message: "Created new user account", data: user }, status: :ok
19 else
20 render json: {status: "ERROR",
21 message: "Could not create new user account",
22 data: user.errors},
23 status: :unprocessable_entity
24
25 end
26 end
27
28 private
29
30 def user_params
31 params.require(:user).permit(:username, :email, :password, :password_confirmation)
32 end
33
34 protected
35
36 def configure_permitted_parameters
37 devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
38 end
39
40 end
41 end
42 end
Any idea why I'm getting this response, and how to fix it? I'm grabbing the email and password from the params hash and passing it into the create function. I don't know why the response I'm getting states that it's empty
You should format your parameters nested under "user" e.g.:
{
"user": {
"username": "username",
"email": "[email protected]",
"password": "password",
"password_confirmation": "password"
}
}
as seen on this answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With