为什么我的 Rails 应用在我的字符数正确时返回 "Password is too short (minimum is 6 characters)"?
Why is my Rails app returning "Password is too short (minimum is 6 characters)" when I have the correct amount of characters?
我正在构建一个简单的注册页面。
这是我的用户模型及其验证和关联:
class User < ApplicationRecord
has_secure_password
has_many :posts
has_many :comments
has_many :likes
validates :username, presence: true, uniqueness: true
validates :password, presence: true, length: { minimum: 6 }
end
这是我的用户迁移:
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name
t.string :username
t.string :password_digest
t.integer :age
t.integer :years_in_the_labor
t.string :title
t.string :location
t.string :ministry
t.timestamps
end
end
end
这也是我的用户控制器创建方法:
def create
@user = User.new(name: params[:name], title: params[:title], username: params[:username], password: [:password])
if @user.valid?
@user.save
render json: { status: 200, user: @user }
else
render json: { status: 401, message: @user.errors.full_messages }
end
end
我正在通过 fetch
发送参数,参数如下:
<ActionController::Parameters {"name"=>"Test Name User", "title"=>"Test Title User", "username"=>"test_username_user", "password"=>"password123", "controller"=>"users", "action"=>"create", "user"=>{"name"=>"Test Name User", "username"=>"test_username_user", "title"=>"Test Title User"}} permitted: false>
我得到这个错误:
message: ["Password is too short (minimum is 6 characters)"]
我的密码验证无效,尽管我传递了正确数量的字符。
你传递给 User.new 只是 [:password] 而不是 params[:password]
它最常用于仅传递参数的私有函数,如下所示:
private
def user_params
params.require(:user).permit(:name, :title, :username, :password)
end
然后在你的创建方法中:
@user = User.new(user_params)
更新
您可以直接检查用户是否已保存,而不是询问用户是否有效
if @user.save
# your code for success
else
# your code for failure
end
忘记密码params[:password]
@user = User.new(name: params[:name], title: params[:title], username: params[:username], password: params[:password])
因为没有匹配到任何密码,所以是空白,少于6个字符。
我正在构建一个简单的注册页面。
这是我的用户模型及其验证和关联:
class User < ApplicationRecord
has_secure_password
has_many :posts
has_many :comments
has_many :likes
validates :username, presence: true, uniqueness: true
validates :password, presence: true, length: { minimum: 6 }
end
这是我的用户迁移:
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name
t.string :username
t.string :password_digest
t.integer :age
t.integer :years_in_the_labor
t.string :title
t.string :location
t.string :ministry
t.timestamps
end
end
end
这也是我的用户控制器创建方法:
def create
@user = User.new(name: params[:name], title: params[:title], username: params[:username], password: [:password])
if @user.valid?
@user.save
render json: { status: 200, user: @user }
else
render json: { status: 401, message: @user.errors.full_messages }
end
end
我正在通过 fetch
发送参数,参数如下:
<ActionController::Parameters {"name"=>"Test Name User", "title"=>"Test Title User", "username"=>"test_username_user", "password"=>"password123", "controller"=>"users", "action"=>"create", "user"=>{"name"=>"Test Name User", "username"=>"test_username_user", "title"=>"Test Title User"}} permitted: false>
我得到这个错误:
message: ["Password is too short (minimum is 6 characters)"]
我的密码验证无效,尽管我传递了正确数量的字符。
你传递给 User.new 只是 [:password] 而不是 params[:password]
它最常用于仅传递参数的私有函数,如下所示:
private
def user_params
params.require(:user).permit(:name, :title, :username, :password)
end
然后在你的创建方法中:
@user = User.new(user_params)
更新
您可以直接检查用户是否已保存,而不是询问用户是否有效
if @user.save
# your code for success
else
# your code for failure
end
忘记密码params[:password]
@user = User.new(name: params[:name], title: params[:title], username: params[:username], password: params[:password])
因为没有匹配到任何密码,所以是空白,少于6个字符。