0

Rails parameters are different actual sending parameters

Parameters sending from the postman:

{
"first_name": "Arun",
"last_name": "Deepak",
"email": "[email protected]",
"password": "Abc@123",
"confirm_pasword": "Abc@123",
"user_type": "seller"
}

Params in controller:

{
"first_name"=>"Arun", "last_name"=>"Deepak",
"email"=>"[email protected]",
"password"=>"[FILTERED]", "confirm_pasword"=>"Abc@123",
"user_type"=>"seller",
"user"=>{"email"=>"[email protected]", "user_type"=>"seller"}
}

Controller:

def create
  @user = User.create!(user_params)
  render json: @user
end
...
def user_params
  params.require(:user).permit(:email, :user_type, :password, :password_confirmation)
end

Rails routes:

namespace :api do
  namespace :v1 do
    resources :users
  end
end

Here, How is the parameter changing in the controller?

I know I'm sending the wrong parameters but How are user_params accepting the wrong parameters?

How the request parameters have been modified?

1 Answer 1

1

The params don't get changed in the controllers. Maybe you are trying to send first_name and last_name as user params. And it is giving you an error during user creation. That's because your user_params method is only returning params that are under the user, using this params.require(:user).

Parameters: {
  "user": {
    "email": "[email protected]",
    "user_type": "seller",
    "first_name": "Arun",
    "last_name": "Deepak",
    "password": "[FILTERED]",
    "confirm_pasword": "Abc@123"
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm sending the wrong parameters but why it's allowing? and How it's modified?
If you’re using Postman then you can easily add params like user[email] or user[first_name]
I'm saying I'm sending the wrong params but the system is accepting how it's possible?
I don't know what’s actually happening in your project 🤷‍♂️

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.