has_secure_password 破坏集合保存 [英] has_secure_password breaks collection save

查看:29
本文介绍了has_secure_password 破坏集合保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改 Rails 应用程序以使用has_secure_password",但这导致了意外问题.

I am modifying a Rails app to use 'has_secure_password', but this has caused unexpected issues.

当用户创建帐户时,他们指定姓名、电子邮件、密码、password_confirmation,并允许选择零个或多个朋友(参见下面的屏幕截图).

When a user creates an account, they specify name, email, password, password_confirmation, and are allowed to choose zero or more friends (see screenshot below).

在我的控制器中,我在保存新用户实体的同时创建了友谊关联.这是通过用一组关联的用户对象填充创建参数的朋友"属性来完成的.

In my controller, I am creating Friendship associations at the same time that the new user entity is saved. This is done by populating the 'friends' attribute of the creation parameters with an array of associated user objects.

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"添加到我的用户模型中.现在,每当我保存模型时,都会收到一条通用的朋友无效"消息.即使知道此消息的来源也会有所帮助.

This method used to work, until adding the "has_secure_password" to my User model. Now, I get a generic "Friends is invalid" message whenever I save my model. Even knowing where this message is coming from would be helpful.

我的项目是托管在 GitHub 上

这是为了破坏我的朋友储蓄而必须进行的唯一更改的差异.

Here is a diff of the only changes I had to make in order to break my Friends saving.

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

推荐答案

问题只是在我的验证中我有

The issue was simply that in my validations I had

validates :password, presence: true

这是在 has_secure_password 中执行的验证的副本.不知道为什么这会导致我看到的意外行为,但删除我的重复验证解决了这些症状.

Which is a duplicate of a validation performed within has_secure_password. No idea why this was causing the unexpected behavior I was seeing, but removing my duplicate validation resolved the symptoms.

这篇关于has_secure_password 破坏集合保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆