I'm trying to send my form data to rails api from my react frontend but this happens
This is my react code to handle form data
constructor(props) {
super(props);
this.user= {};
this.state = this.getInitialState();
}
getInitialState = () => {
const initialState = {
email: "",
password: ""
};
return initialState;
}
change = e => {
this.setState({
[e.target.name]: e.target.value
});
};
onSubmit = (e) => {
e.preventDefault();
this.user = JSON.stringify(this.state)
this.user = "{\"user\":" + this.user + "}"
console.log(this.user);
fetch('http://localhost:3001/v1/users', {
method: 'POST',
body: this.user
}).then(function(response) {
console.log(response.json());
})
this.setState(this.getInitialState());
}
This code gives the following string in console:
{"user":{"email":"[email protected]","password":"password123."}}
Rails:
class V1::UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
render :create
else
head(:unprocessable_entity)
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
Doing the same on PAW gives positive results
Please help. I've been trying to fix this for 2 days now.
Thank you in advance.