0

I'm creating a new distributed application using React for the frontend and Rails 5 for the backend. I'm new to Rails, so I couldn't figure out a solution for the issue I'm running into.

The issue is that the params var in Rails controller does not include the variables sent in the POST request by the React frontend.

It gives me the following error:

ActionController::ParameterMissing (param is missing or the value is empty: user):

Which is generated by this line in the Rails controller:

params.require(:user).permit(:name, :cnpj, :email)

This is the body (params) of the POST request made by fetch:

user: {
  cnpj: '1231412',
  email: 'henio@hen.io',
  name: 'henio'
}

The frontend related code:

export const post = async (resource, data) => {
  try{
    const url = `${apiBase}/${resource}`;
    const response = await fetch(url, {
      method: 'POST',
      mode: 'cors',
      cache: 'no-cache',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data),
    });
    if (!response.ok) return false;
    return await response.json();
  }catch(e){
    console.log(e);
    return false;
  }
}

const res = await post(apiResourse,{ user, });

And this is the params object printed in the user_params method inside the controller:

{"controller"=>"api/v1/users", "action"=>"create"}

What the does that mean?

user229044
  • 232,980
  • 40
  • 330
  • 338
heniotierra
  • 108
  • 9

0 Answers0