2013-03-26 55 views
0

將日誌概念應用於我的圖書目錄顯示時,當用戶陷入困境時,我即將發生這種錯誤。ActiveModel :: MassAssignmentSecurity :: UsersController中的錯誤#創建

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

而且我在app /型號編碼/ user.rb如下:

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

而且我在app/contollers/user_controller.rb

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

     respond_to do |format| 
     if @user.save 
     format.html { redirect_to users_url, :notice => 'User #{@user.name} 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 

創建方法的代碼請任何幫助!

回答

4

如果你想在指定你做的方式這些值,你需要將它們添加到attr_accessible在你的模型:

attr_accessible :name, :password_digest, :password, :password_confirmation 

我懷疑你可能不希望同時將與那些,所以你可能要首先從哈希刪除(控制器):

user = params[:user] 
user.delete(:password_confirmation) 
@user = User.new(user) 

你也可以創建一個包含只是想用它來創建新的User值的新的哈希值,如果你只有幾個值,但很多o f值忽略。

(你也可以創建一個新的「空」 User,只是分配給你想要的價值觀 - 如果讓你的情況更有意義)

+0

感謝您的解決方案。再次發生的事情是,當用戶註冊時,當前頁面被移動到user_url,其中顯示用戶列表...這就是它顯示的內容。 用戶#{@user.name}已成功創建。 用戶名不顯示! – user2211662 2013-03-26 13:34:54

相關問題