简体   繁体   English

密码,Password_Confirmation ActiveModel :: MassAssignmentSecurity :: Error:

[英]Password, Password_Confirmation ActiveModel::MassAssignmentSecurity::Error:

I'm working through the Rails Tutorial and I've gotten stuck. 我正在通过Rails教程工作,但遇到了麻烦。 I'm trying to use a password and password_confirmation. 我正在尝试使用密码和password_confirmation。

I'm getting the error(s): 我收到错误:

  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 

All of the errors are for the same reason. 所有错误都是出于相同原因。

User.rb User.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

user_spec.rb user_spec.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) }

Any ideas would be appreciated. 任何想法,将不胜感激。

Add password, :password_confirmation to attr_accessible in user.rb user.rb attr_accessible password, :password_confirmation添加到attr_accessible

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

attr_accessible method takes list of attributes to be accessible. attr_accessible方法获取可访问的属性列表。 the other attribute will be protected see Mass Assignment for the reason. 其他属性将受到保护,原因请参见“ 批量分配 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM