2012-08-10 3 views
3

마이그레이션대량 할당 레일에 3.2.3

을 특성을 보호 할 수 없습니다
class CreateUsers < ActiveRecord::Migration 
    def change 
    create_table :users do |t| 
     t.string :name 
     t.string :password_digest 

     t.timestamps 
    end 
    end 
end 

모델

class User < ActiveRecord::Base 
    attr_accessible :name, :password_digest 
    validates :name, :presence => true, :uniqueness => true 
    has_secure_password 
end 

사용자 등록 _form

.main_form 
    = form_for @user do |f| 

    %div 
     = f.label :name 
     = f.text_field :name, :size=>40 

    %div 
     = f.label :password, "Password" 
     = f.password_field :password 

    %div 
     = f.label :password_confirmation, "Confirmation" 
     = f.password_field :password_field 

    %div 
     = f.submit 'Create user' 

나는 새로운 사용자를 등록하려고 예외가 throw됩니다.

ActiveModel::MassAssignmentSecurity::Error in UsersController#create 

Can't mass-assign protected attributes: password, password_field 

내가 뭘 잘못 했니?

답변

5

has_accessible 목록에이 두 필드를 추가해야합니다. Rails는 대량 할당에서 데이터베이스 필드를 보호 할뿐만 아니라 이러한 '가상'필드와 같은 모든 필드도 보호합니다. (대신 attr_accesible 라인의) 사용자 모델에서

:

또한
attr_accessible :name, :password_field, :password 

, password_digest는 어떤 경우에도 수정은 가능하지 않아야, 즉 계산 된 필드하지 사용자 입력입니다.

+0

어떤 파일을 추가해야합니까? – Alexandre

+0

죄송 합니다만, 'User' 모델에 있어야한다는 것을 잊어 버렸습니다. 'attr_accessible' 라인을 광산으로 바꾸면 작동 할 것입니다. – Matzi

관련 문제