我试图在我的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":"caio@email.com","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"]}
发布于 2018-03-02 04:38:12
使用curl命令时,您没有正确发送params散列。
根据您允许的强参数方法def user_params
,使用curl -H "Content-Type: application/json" -X POST -d '{"user": {"email":"caio@email.com","password":"123456","name":"Caio A","age":"18"}}' http://localhost:3000/users
或这些方法
https://stackoverflow.com/questions/49062857
复制