Ad

Parameter Missing From Frontend Request To Backend

I am currently getting this error:

ActionController::ParameterMissing in UsersController#create
param is missing or the value is empty: first_name

I am trying to create a User record with my Rails backend API. I am filtering requests with strong params like so

def create
  user = User.new(user_params)
  ...
end

def user_params
  params.require([:first_name, :last_name, :email, :password])
end

In my frontend I am making this type of request. I double checked to make sure with the snipper below, the object I am attempting to send is like so:

data:

{
  "first_name": "asdasd",
  "last_name": "asdasd",
  "email": "[email protected]",
  "password": "password"
}

snippet:

  const response = await fetch(url, {
    method: "POST",
    body: JSON.stringify({
      first_name,
      last_name,
      email,
      password,
    }),
  });
Ad

Answer

Hey code looks good to me, however I would try to to define the content type json with property headers

  const response = await fetch(url, {
    method: "POST",
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      first_name,
      last_name,
      email,
      password,
    }),
  });

I'm not super familiar with fetch yet but often there is black magic that operates when you specify the content type

Ad
source: stackoverflow.com
Ad