gpt4 book ai didi

ruby-on-rails - 密码,Password_Confirmation ActiveModel::MassAssignmentSecurity::Error:

转载 作者:行者123 更新时间:2023-11-28 21:31:30 26 4
gpt4 key购买 nike

我正在学习 Rails 教程,但遇到了困难。我正在尝试使用密码和 password_confirmation。

我遇到错误:

  15) User when password confirmation is nil 
Failure/Error: @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")
ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: password, password_confirmation
# ./spec/models/user_spec.rb:5:in `new'
# ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.21758 seconds
25 examples, 15 failures

Failed examples:

rspec ./spec/models/user_spec.rb:8 # User
rspec ./spec/models/user_spec.rb:9 # User
rspec ./spec/models/user_spec.rb:10 # User
rspec ./spec/models/user_spec.rb:11 # User
rspec ./spec/models/user_spec.rb:12 # User
rspec ./spec/models/user_spec.rb:14 # User
rspec ./spec/models/user_spec.rb:17 # User when name is not present
rspec ./spec/models/user_spec.rb:21 # User when name is too long
rspec ./spec/models/user_spec.rb:25 # User when email format is invalid should be invalid
rspec ./spec/models/user_spec.rb:33 # User when email format is invalid when email format is valid should be valid
rspec ./spec/models/user_spec.rb:47 # User when email address is already taken
rspec ./spec/models/user_spec.rb:55 # User when email address is already taken
rspec ./spec/models/user_spec.rb:59 # User when password is not present
rspec ./spec/models/user_spec.rb:63 # User when password doesn't match confirmation
rspec ./spec/models/user_spec.rb:67 # User when password confirmation is nil

所有的错误都是出于同样的原因。

用户.rb

class User < ActiveRecord::Base
attr_accessible :email, :name
before_save { |user| user.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
#has_secure_password
has_many :event
end

用户规范.rb

require 'spec_helper'

describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }

如有任何想法,我们将不胜感激。

最佳答案

user.rb 中的 attr_accessible 添加 password, :password_confirmation

attr_accessible :name, :email, :password, :password_confirmation

attr_accessible 方法采用可访问的属性列表。其他属性将受到保护,请参阅 Mass Assignment原因。

关于ruby-on-rails - 密码,Password_Confirmation ActiveModel::MassAssignmentSecurity::Error:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20174389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com