2013-11-27 68 views
1

我正在修改Rails應用程序以使用'has_secure_password',但這已經導致意外的問題。has_secure_password破解集合保存

當用戶創建一個帳戶,指定他們的姓名,電子郵件,密碼,password_confirmation,並允許選擇零級或更多的朋友(見下圖)。

Screenshot of User Add Form

在我的控制,我在同一時間,新的用戶實體保存創建友好協會。這是通過用相關用戶對象的數組填充創建參數的'朋友'屬性來完成的。

def create 
checked_params = user_params 
if checked_params[:friends] 
    checked_params[:friends].delete "" 
    checked_params[:friends] = checked_params[:friends].map { |id| User.find(id) } 
end 

@user = User.new(checked_params) 

respond_to do |format| 
    if @user.save 
    format.html { redirect_to @user, notice: 'User was successfully created.' } 
    format.json { render action: 'show', status: :created, location: @user } 
    else 
    @friends = user_params[:friends] # Replace with simple names and IDs 
    format.html { render action: 'new' } 
    format.json { render json: @user.errors, status: :unprocessable_entity } 
    end 
end 
end 

此方法用於工作,直到將「has_secure_password」添加到我的用戶模型。現在,每當我保存我的模型時,我都會得到一個通用的「朋友無效」消息。即使知道這個消息來自哪裏也是有幫助的。

我的項目是hosted on GitHub

這裏是只有我才能打破好友節能做出改變的差異。

diff --git a/Gemfile b/Gemfile 
index 9beb2e3..2905724 100644 
--- a/Gemfile 
+++ b/Gemfile 
@@ -36,7 +36,7 @@ group :doc do 

# Use ActiveModel has_secure_password 
-# gem 'bcrypt-ruby', '~> 3.0.0' 
+ gem 'bcrypt-ruby', '~> 3.0.0' 

diff --git a/app/models/user.rb b/app/models/user.rb 
index b5e6674..6d9cec9 100644 
--- a/app/models/user.rb 
+++ b/app/models/user.rb 
@@ -23,5 +23,6 @@ class User < ActiveRecord::Base 
- validates_confirmation_of :password 
+ 
+ has_secure_password 


diff --git a/db/migrate/20131018155801_create_users.rb b/db/migrate/201310181558 
index 3b4a508..2a8cd85 100644 
--- a/db/migrate/20131018155801_create_users.rb 
+++ b/db/migrate/20131018155801_create_users.rb 
@@ -3,7 +3,7 @@ class CreateUsers < ActiveRecord::Migration 
    create_table :users do |t| 
     t.string :name, null: false 
     t.string :email, null: false 
-  t.string :password, null: false 
+  t.string :password_digest, null: false 
     t.string :role, null: false 

     t.timestamps 
+0

這個問題只是,在我驗證我有 驗證:密碼,存在:真 這是has_secure_password內進行驗證的副本。 不知道爲什麼這會導致我看到的意外行爲,但刪除我的驗證可以解決症狀。 –

回答

0

的問題只是,在我驗證我有

validates :password, presence: true 

這是has_secure_password內進行驗證的副本。 不知道爲什麼這會導致我看到的意外行爲,但刪除我的重複驗證可以解決症狀。