首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >密码,Password_Confirmation ActiveModel::MassAssignmentSecurity::Error:

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

Stack Overflow用户
提问于 2013-11-24 12:22:06
回答 1查看 259关注 0票数 0

我正在学习Rails教程,我被困住了。我正在尝试使用密码和password_confirmation。

我得到了错误:

代码语言:javascript
复制
  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 

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

User.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
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) }

任何想法都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-24 12:25:46

password, :password_confirmation中将attr_accessible添加到user.rb

代码语言:javascript
复制
attr_accessible :name, :email, :password, :password_confirmation

attr_accessible方法接受可访问的属性列表。另一个属性将受到保护,请参见质量分配

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20174389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档