1

我不知道为什么我会收到以下关于大规模分配保护的错误。虽然我有配置设置为 config.active_record.whitelist_attributes = false

以下是模型和控制器的代码

有任何想法吗?

      ActiveModel::MassAssignmentSecurity::Error in UsersController#create 
Can't mass-assign protected attributes: password_confrimation
Rails.root: C:/Users/huzaifa.gain/My Documents/Aptana Studio 3 Workspace/blog

Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:7:in `new'
app/controllers/users_controller.rb:7:in `create'


Request

Parameters: 
{"utf8"=>"?",
 "authenticity_token"=>"CIPrmS9ZZlgUELOMkAT6Htw1eLPMxmXRh7Ur7doeYcY=",
 "user"=>{"email"=>"huzi",
 "password"=>"[FILTERED]",
 "password_confrimation"=>"[FILTERED]"},
 "commit"=>"Create User"}


class UsersController < ApplicationController
  def new 
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
       redirect_to_ articles_path, :notice => 'User successfully added.'
     else 
       render :action =>'new'
    end
  end

  def edit
    @user = User.find(parms[:id]) 
  end

  def update
    @user = User.find(parms[:id])
    if @user.update_attributes(params[:user])
      redirect_to articles_path, :notice => 'Updated user information successfully'
    else 
      render :action => 'edit'
    end
  end
end


require 'digest'
class User < ActiveRecord::Base

  attr_accessor  :email, :password , :password_confirmation
  attr_accessible :email, :password , :password_confirmation
  validates :email, :uniqueness => true, 
                                    :length => {:within => 5..50},
                                    :presence => true

  validates :password, :confirmation => true, :length => { :within => 4..20 }, :presence => true, :if => :password_required?

  has_one :profile

  has_many :articles, :order => 'published_at DESC, title ASC',
                      :dependent => :nullify
  has_many :replies, :through => :articles, :source => :comments

  before_save :encrypt_new_password

  def self.authenticate(email, password)
    user = find_by_email(email)
        return user if user && user.authenticated?(password)
  end

  def authenticated?(password)
    self.hashed_password == encrypt(password)
  end


  def encrypt_new_password
    return if password.blank?
      self.hashed_password  = encrypt(password)                                                                                                                                                                                                                                                                                                                                                                                                                                                              
  end

  def password_required?
    hashed_password.blank? || password.present?
  end

 def encrypt(string)
   Digest::SHA2.hexdigest(string)
 end 


end
4

2 回答 2

5

我认为这是因为一个错字。您的模型有attr_accessible :email, :password , :password_confirmation,但看起来您的表单拼写为 password_confrimation。

于 2012-05-20T13:46:09.293 回答
0

更改您的表格:

:password_confrimation

至:

:password_confirmation

这是一个错字。

于 2012-05-20T14:05:08.303 回答