So I'm getting this error for whatever reason. It says that the parameter "User" is missing, but I can see it pretty clearly there. Also, I'm not sure why the "user" parameter is listed twice with the second being empty, but I assume that's part of what's causing it.
Started POST "/users" for 127.0.0.1 at 2018-08-30 21:56:48 -0700
Processing by UsersController#create as HTML
Parameters: {"params"=>{"user"=>{"username"=>"fsda", "first_name"=>"fdsa", "last_name"=>"fds", "email"=>"fdsa", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}, "headers"=>{"Content-Type"=>"application/json"}, "user"=>{}}
Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: user):
app/controllers/users_controller.rb:18:in `user_params'
app/controllers/users_controller.rb:4:in `create'
Here's the piece of code that's making the call from a separate server (React client)
25 ax.post('/users', { |~
26 params: { |~
27 user: this.state |~
28 }, |~
29 headers: { |~
30 "Content-Type": 'application/json' |~
31 } |~
32 })
ax is defined here in a separate file:
1 import axios from 'axios';
2
3 export default axios.create({
4 baseURL: 'http://localhost:3000'
5 })
And here's the Rails controller
3 def create
4 @user.new(user_params)
5 if @user.save
6 json_response(@user, :created)
7 end
8 end
9
10 def show
11 @user = User.find(params[:id])
12 render json: @user
13 end
14
15 private
16
17 def user_params
18 params.require(:user).permit(:username, :email, :first_name, :last_name, :password_digest)
19 end
Sorry about the poor formatting. Copy-pasting from vim is really awkward unless someone knows a better way to do that.
SOLUTION:
Change @user.new(user_params) to @user = User.new(user_params
Change params: { user: {...} } to user: {...}