【问题标题】:validation fails on SecurePassword Virtual Attributes on custom create at Activerecord Model在 Activerecord 模型上自定义创建的 SecurePassword 虚拟属性验证失败
【发布时间】:2014-02-04 09:46:33
【问题描述】:

我正在尝试将基于 SecurePassword 的自定义用户身份验证机制与通过omniauth-facebook gem 进行的 Facebook 集成相结合。

我的应用使用 Ruby 2.0.0 和 Rails 4.0.0。

我尝试按照本指南 omniauth 和其他一些文章为用户和身份验证模型提出类似的内容

用户模型:

class User < ActiveRecord::Base

has_one :user_playlist
has_one :user_info

has_many :band_likes
has_many :song_likes

has_many :band_comments
has_many :song_comments

has_many :authorizations

#many to many relation between User and Band
#todo: make a bands_users migration
has_and_belongs_to_many :bands


has_secure_password

validates :username, presence: true, uniqueness: {case_sensitive: false}, length: {in: 8..64}, format: {with: /\A[a-zA-Z ]+\Z/, message: 'Debe poseer solo letras y espacios.'}
validates :email, presence: true, uniqueness: {case_sensitive: false}, format: {with: /@/, message: 'Dirección de correo inváilda.'}
validates :password, length: {in: 8..24}
validates :password_confirmation, length: {in: 8..24}

def self.create_from_hash!(hash)
        create(:email => hash['info']['email'], :username => hash['info']['name'], :password => hash['uid'], :password_confirmation => hash['uid'] )
end
end

授权模型:

class Authorization < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :uid, :provider
validates_uniqueness_of :uid, :scope => :provider

def self.find_from_hash(hash)
  find_by_provider_and_uid(hash['provider'], hash['uid'])
end

def self.create_from_hash(hash, user = nil)
  user ||= User.create_from_hash!(hash)
  Authorization.create(:user => user, :uid => hash['uid'], :provider => hash['provider'])
end
end

会话控制器

class SessionsController < ApplicationController

def create
  auth = request.env['omniauth.auth']
  unless @auth = Authorization.find_from_hash(auth)
    # Create a new user or add an auth to existing user, depending on
    # whether there is already a user signed in.
    @auth = Authorization.create_from_hash(auth, current_user)
  end
  # Log the authorizing user in.
  self.current_user = @auth.user

  render :text => "Welcome, #{current_user.username}. <br />User saved = #{current_user.save} .<br/>User valid = #{current_user.valid?}.<br />errors= #{current_user.errors.full_messages}"
end

end

最后一次渲染是为了检查我的密码没有得到验证的事实,不管我使用 hash['uid']、hash['info']['name'] 还是其他什么.

我使用这个值的原因只是因为,我稍后会弄清楚如何为 oauth-ed 用户构建一个随机密码,但我不想要空白密码也不禁用验证。

但是,无论我使用什么值,始终只获取我的姓名和电子邮件:

*Welcome, "My Real Name Here.
User saved = false.
User valid = false.
errors= ["Password is too short (minimum is 8 characters)", "Password confirmation is too short (minimum is 8 characters)"]*

在 Rails 控制台中创建用户时没有问题,就在 OAuth 尝试使用 create_from_hash 创建用户时。

另外,如果我尝试将哈希值中的不存在值分配给密码字段,它会添加可以为空白的消息。所以,它不是空白的。

在控制器中渲染 hash['uid'] 表明它比 8 长。

我必须警告我是 Rails 新手,所以如果可以的话,请用苹果 xD 解释我

提前致谢!

【问题讨论】:

    标签: ruby-on-rails activerecord omniauth activemodel


    【解决方案1】:

    最后我在用户模型上想出了这个:

    def self.create_from_hash!(hash)
       self.where(:email => hash.info.email.to_s).first_or_create do |user|
          user.email = hash.info.email
          user.username = hash.info.name
          user.password = hash.uid
          user.password_confirmation = hash.uid
       end
    end
    

    我不知道为什么后者不起作用,但至少这个起作用!

    您好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多