在 Rails 中创建新用户时,我一直收到 "password can't be blank" 错误
I keep getting "password can't be blank" error when creating a new user in Rails
我正在 Rails API 中创建我认为是简单的用户身份验证。我已经搜索了在这里可以找到的所有答案,但我根本无法弄清楚自己做错了什么。无论如何,当我尝试创建新用户时,我收到错误 "password can't be blank."
这是我的控制器代码:
class UsersController < ApplicationController
def create
user = User.new(user_params)
if user.save
render json: {user: user}, status: 201
else
render json: {errors: user.errors.full_messages}
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
我的模型,经过验证:
class User < ApplicationRecord
validates :name, presence: true, length: {maximum: 50}
validates :email, presence: true, length: {maximum: 255}, uniqueness: true
has_secure_password
validates :password, presence: {on: :create}, confirmation: {case_sensitive: true}, length: {minimum: 8}
validates :password_confirmation, presence: true
end
一个不断被拒绝的测试JSON对象:
{
"name": "Jim Nobody",
"email": "jim@anywhere.com",
"password": "abc12345",
"password_confirmation": "abc12345"
}
我不断收到的错误:
{
"errors": [
"Password can't be blank",
"Password can't be blank",
"Password is too short (minimum is 8 characters)",
"Password confirmation can't be blank"
]
}
我知道这个问题还有其他的答案,但是我一行一行的梳理了一下,看不出自己做错了什么。据我所知,所有字段都是允许的并且拼写正确。
如果有任何帮助,我将不胜感激。谢谢!
我知道这很难直接回答您的问题,但我认为它会有所帮助。使用此 gem 称为 byebug。基本上,安装它,然后将 byebug
放在控制器中用户保存之前。当它执行代码时,它将挂起服务器,直到您在服务器选项卡中键入继续。在您键入继续之前,您可以像使用 rails 控制台一样使用服务器选项卡并调试参数的当前状态等。希望这可以帮助您调试问题。
您好,请检查控制器中的参数。目前您只传递参数。参数将是用户。只需尝试使用用户参数。
{ "users": { "name":"test" } }
感谢 Rajkumar 的回答。最初,我的前端发送了一个 JSON 对象,如下所示:
{
"name": "Someone",
"password": "a_password",
...
}
但是 Rails 期待一个被包裹在 user
散列中的一个像这样的:
{
"user": {
"name": "Somebody",
"password": "a_password",
...
}
}
谢谢大家的帮助!
我正在 Rails API 中创建我认为是简单的用户身份验证。我已经搜索了在这里可以找到的所有答案,但我根本无法弄清楚自己做错了什么。无论如何,当我尝试创建新用户时,我收到错误 "password can't be blank."
这是我的控制器代码:
class UsersController < ApplicationController
def create
user = User.new(user_params)
if user.save
render json: {user: user}, status: 201
else
render json: {errors: user.errors.full_messages}
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
我的模型,经过验证:
class User < ApplicationRecord
validates :name, presence: true, length: {maximum: 50}
validates :email, presence: true, length: {maximum: 255}, uniqueness: true
has_secure_password
validates :password, presence: {on: :create}, confirmation: {case_sensitive: true}, length: {minimum: 8}
validates :password_confirmation, presence: true
end
一个不断被拒绝的测试JSON对象:
{
"name": "Jim Nobody",
"email": "jim@anywhere.com",
"password": "abc12345",
"password_confirmation": "abc12345"
}
我不断收到的错误:
{
"errors": [
"Password can't be blank",
"Password can't be blank",
"Password is too short (minimum is 8 characters)",
"Password confirmation can't be blank"
]
}
我知道这个问题还有其他的答案,但是我一行一行的梳理了一下,看不出自己做错了什么。据我所知,所有字段都是允许的并且拼写正确。
如果有任何帮助,我将不胜感激。谢谢!
我知道这很难直接回答您的问题,但我认为它会有所帮助。使用此 gem 称为 byebug。基本上,安装它,然后将 byebug
放在控制器中用户保存之前。当它执行代码时,它将挂起服务器,直到您在服务器选项卡中键入继续。在您键入继续之前,您可以像使用 rails 控制台一样使用服务器选项卡并调试参数的当前状态等。希望这可以帮助您调试问题。
您好,请检查控制器中的参数。目前您只传递参数。参数将是用户。只需尝试使用用户参数。
{ "users": { "name":"test" } }
感谢 Rajkumar 的回答。最初,我的前端发送了一个 JSON 对象,如下所示:
{
"name": "Someone",
"password": "a_password",
...
}
但是 Rails 期待一个被包裹在 user
散列中的一个像这样的:
{
"user": {
"name": "Somebody",
"password": "a_password",
...
}
}
谢谢大家的帮助!