1
votes

I'm trying to create app with Rails as an API provider and stuck a bit during seting my user authentication process. Whenever I try to create a new user by Postman I get a message that my controller cannot recognize user params and rise an error:

Started POST "/api/v1/create" for ::1 at 2020-02-23 02:36:29 +0100 Processing by Api::V1::UsersController#create as / Parameters: {"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "username"=>"Andrzej Strzelba", "email"=>"[email protected]"} {"password"=>"qwertyUI1", "password_confirmation"=>"qwertyUI1", "username"=>"Andrzej Strzelba", "email"=>"[email protected]", "controller"=>"api/v1/users", "action"=>"create"} Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: user): app/controllers/api/v1/users_controller.rb:28:in user_params' app/controllers/api/v1/users_controller.rb:8:increate'

and my controller:

    class UsersController < ApplicationController
    skip_before_action :verify_authenticity_token

    def create
        puts params
        @user = User.new(user_params)
        if @user.save
            render json: { status: :created, user: @user}
        else
            render json: {status: 500, errors: @user.errors.full_messages }
        end
    end

    private
    def user_params
        params.require(:user).permit(:username, :email, :password, :password_confirmation)
    end 

    end

I am almost sure that problem is caused by a lack of wrapping my params with "user" key. I mean user: {email: '...', username: '...' ect} but I am not excactly sure how to solve it on Postman. I tried to send below JSON as well. But it did not succed as well:

{
    "user":
    {
        "username": "Andrzej Strzelba",
        "email": "[email protected]",
        "passowrd": "qwertyUI1!",
        "password_confirmation": "qwertyUI1!"
    }
} 

How can I fix it?

3

3 Answers

3
votes

Your code looks good. First, let assume you were using postman incorrectly. Have you tried this?

Below url, you can find: Body >> raw >> JSON (look at the picture)

postman body send json

Else, you can try to remove the require(:user), so it becomes:

def user_params
  params.permit(:username, :email, :password, :password_confirmation)
end

And send the JSON:

{
    "username": "Andrzej Strzelba",
    "email": "[email protected]",
    "passowrd": "qwertyUI1!",
    "password_confirmation": "qwertyUI1!"
}
1
votes

You shouldn't be passing your params as an user object, send it without the user keyword. Like this

{
        "username": "Andrzej Strzelba",
        "email": "[email protected]",
        "passowrd": "qwertyUI1!",
        "password_confirmation": "qwertyUI1!"
}

Then it should work.

1
votes

Try Posting your users params like this and Add access token in authorization.

{
    "username": "Andrzej Strzelba",
    "email": "[email protected]",
    "passowrd": "qwertyUI1!",
    "password_confirmation": "qwertyUI1!"
}