2013-03-16 55 views
0

我正在使用authlogic進行身份驗證並使用this教程。我也參考了rubyDocs。如果我正確理解attr_protected方法確保屬性列表不能用於批量分配。我的模型屬性受到保護。我也嘗試將config/application.rb中的白名單標記更改爲false ..這沒有任何區別。使用authlogic進行登錄時出錯::無法批量分配受保護的屬性:

我認爲這個問題可能是因爲在用戶模型中沒有password_confirmation屬性,稱爲密碼&。這就是本教程所說的

我們將字段名稱從:crypted_pa​​ssword更改爲:password。 對 進行哈希處理後,Authlogic將把:password字段映射到:crypted_pa​​ssword。我們還將字段類型從f.text_field更改爲 f.password_field,這將創建標準密碼輸入字段 而不是純文本輸入字段。我們還添加了一個 :password_confirmation字段。所有支持這些字段的邏輯 都內置到authlogic中。

這是真的嗎?有關如何解決此問題的任何建議?

的Rails:3.2.12

紅寶石:1..9.3

ActiveModel::MassAssignmentSecurity::Error in UsersController#create 

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

{"utf8"=>"✓", 
"authenticity_token"=>"WUw09PvSlIxLUBFFsi1hiK6v0Y3nn7wqkjH3seCkU34=", 
"user"=>{"username"=>"test", 
"email"=>"test", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]"}, 
"commit"=>"Create User"} 

以下是我的模型&控制器

模型

class User < ActiveRecord::Base 
    attr_accessible :crypted_password, :email, :password_salt, :persistence_token, :username 
end 

控制器

def create 
    @user = User.new(params[:user]) 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render json: @user, status: :created, location: @user } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

FORM.html

div class="field"> 
    <%= f.label :username %><br /> 
    <%= f.text_field :username %> 
    </div> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 
    <div class="field"> 
    <%= f.label :password %><br /> 
    <%= f.password_field :password %> 
    </div> 
    <div class="field"> 
     <%= f.label :password_confirmation %><br /> 
     <%= f.password_field :password_confirmation %> 
    </div> 

感謝

回答

1

看來,就像你說的,你的屬性受到保護,使他們不能大規模分配。讓他們通過向attr_accessible列表

class User < ActiveRecord::Base 
    attr_accessible :crypted_password, :email, :password_salt, :persistence_token, :username, :password, :password_confirmation 
end 

注意訪問:您可能還希望從列表中刪除敏感數據,如:crypted_password:password_salt

class User < ActiveRecord::Base 
    attr_accessible :email, :persistence_token, :username, :password, :password_confirmation 
end 
相關問題