我正在尝试在我的API中发送POST请求(以创建用户),但我总是得到所有密码验证错误,因为我的密码来自API作为过滤器。
我不明白这个错误,因为如果我从控制台创建用户并尝试登录我的API(使用电子邮件和密码进行JWT身份验证),密码也是过滤的,但一切正常。
我想保持密码过滤。
我的用户创建方法:
# POST /users
def create
@user = User.new(user_params)
if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
我的文件filter_parameter_logging.rb:
# Be sure to restart your server when you modify this file.
# Configure sensitive parameters which will be filtered from the log file.
Rails.application.config.filter_parameters += [:password]
用于创建用户的Curl命令:
curl -H "Content-Type: application/json" -X POST -d '{"email":"[email protected]","password":"123456","name":"Caio A","age":"18"}' http://localhost:3000/users
请求回复:
{"password":["can't be blank","Password is required","Password minimum size is 4 characters"]}
使用curl命令,您没有正确发送params哈希。
curl -H "Content-Type: application/json" -X POST -d '{"user":
{"email":"[email protected]","password":"123456","name":"Caio A","age":"18"}}' http://localhost:3000/users
或沿着那些线,根据你允许的强大的params方法def user_params