[英]Ruby on Rails: I cannot create a new user
When I sent to the terminal with $ curl -X POST '{"user": {"name": "hogehoge", "email": "hogehoge@gmail.com", "icon_url": "hogehoge", "location": "hogehgoe", "password": "hogehoge"}}' localhost:3000/v1/users
on my application directory for creating a new user, I had an error. 当我使用
$ curl -X POST '{"user": {"name": "hogehoge", "email": "hogehoge@gmail.com", "icon_url": "hogehoge", "location": "hogehgoe", "password": "hogehoge"}}' localhost:3000/v1/users
发送到终端时, $ curl -X POST '{"user": {"name": "hogehoge", "email": "hogehoge@gmail.com", "icon_url": "hogehoge", "location": "hogehgoe", "password": "hogehoge"}}' localhost:3000/v1/users
我的应用程序目录上$ curl -X POST '{"user": {"name": "hogehoge", "email": "hogehoge@gmail.com", "icon_url": "hogehoge", "location": "hogehgoe", "password": "hogehoge"}}' localhost:3000/v1/users
用于创建新用户,我遇到了错误。
{
"status": 400,
"error": "Bad Request",
"exception": "#<ActionController::ParameterMissing: param is missing or the value is empty: user>",
・・・
・・・
・・・
}
at the server log 在服务器日志
Started POST "/v1/users" for 127.0.0.1 at 2018-12-11 16:55:56 +0900
Processing by V1::UsersController#create as JSON
Parameters: {"name"=>"hogehoge", "email"=>"hogehoge@gmail.com", "icon_url"=>"hogehoge", "location"=>"hogehoge", "password"=>"[FILTERED]"}
Completed 400 Bad Request in 1ms
ActionController::ParameterMissing (param is missing or the value is empty: user):
app/controllers/v1/users_controller.rb:52:in `user_params'
app/controllers/v1/users_controller.rb:20:in `create'
I thought user_params was satisfied with correct parameters, but it didnot. 我以为user_params对正确的参数感到满意,但事实并非如此。
This is my user_controller.rb
这是我的
user_controller.rb
module V1
class UsersController < ApiController
before_action :set_user, only: [:show, :update, :destroy]
skip_before_action :authenticate_user_from_token!, only: [:show, :index, :create]
# POST /users
def create
@user = User.new(user_params)
if @user.save
render json: @user, serializer: V1::UserSerializer
else
render json: @user.errors, status: :unprocessable_entity
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:name, :email, :location, :icon_url, :password)
p params
end
end
end
why did HTTP sent to me with 400 bad status code? 为什么HTTP向我发送了400个错误的状态代码?
As you said, I sent to the terminal with this JSON params to localhost:3000/v1/users
. 如您所说,我将带有此JSON参数的终端发送到
localhost:3000/v1/users
。
{
"user":
{
"name": "hogehoge",
"email": "hogehoge@gmail.com",
"icon_url": "hogehoge",
"location": "hogehoge",
"password": "hogehoge"
}
}
but now HTTP status sent 500 internal server error to me. 但是现在HTTP状态向我发送了500个内部服务器错误。 This is a new error code.
这是一个新的错误代码。
Started POST "/v1/users" for 127.0.0.1 at 2018-12-11 17:33:50 +0900
Processing by V1::UsersController#create as JSON
Parameters: {"user"=>{"name"=>"hogehoge", "email"=>"hogehoge@gmail.com", "icon_url"=>"hogehoge", "location"=>"hogehoge", "password"=>"[FILTERED]"}}
<ActionController::Parameters {"user"=><ActionController::Parameters {"name"=>"hogehoge", "email"=>"hogehoge@gmail.com", "icon_url"=>"hogehoge", "location"=>"hogehoge", "password"=>"hogehoge"} permitted: false>, "format"=>:json, "controller"=>"v1/users", "action"=>"create"} permitted: false>
Completed 500 Internal Server Error in 34ms (ActiveRecord: 2.0ms)
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
app/controllers/v1/users_controller.rb:20:in `create'
Your View 您的看法
You are not passing any hash having key user
in your params, 您不会在参数中传递任何具有关键
user
哈希,
Parameters: {"name"=>"hogehoge", "email"=>"hogehoge@gmail.com", "icon_url"=>"hogehoge", "location"=>"hogehoge", "password"=>"[FILTERED]"}
If you are creating your user by form by form_for , it will look like, 如果您是通过form_for的表单创建用户的,则它看起来像是,
Parameters: { "user" => { "name"=>"hogehoge", "email"=>"hogehoge@gmail.com", "icon_url"=>"hogehoge", "location"=>"hogehoge", "password"=>"[FILTERED]" } }
You are passing user params by input tag for sure. 您肯定会通过输入标签传递用户参数。 So how you are supposed to use strong parameters!
因此,您应该如何使用强参数!
It looks like you left debug code in your #user_params so it won't return the right object: 好像您在#user_params中留下了调试代码,因此它不会返回正确的对象:
# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:name, :email, :location, :icon_url, :password)
p params
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.