gpt4 book ai didi

ruby-on-rails - 设计:尝试设置用户名字段

转载 作者:行者123 更新时间:2023-11-29 11:57:06 24 4
gpt4 key购买 nike

我正在尝试使用本教程使用 Devise 设置用户名字段:https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address

登录时,登录字段工作正常,但当我尝试重置密码或重新发送确认消息时,我收到 postgres 错误:

PG::UndefinedColumn: ERROR: column users.login does not exist LINE 1: SELECT "users".*      FROM "users" WHERE "users"."login" = 'nah... ^ : SELECT "users".* FROM "users" WHERE "users"."login" = 'nahtnam' ORDER BY "users"."id" ASC LIMIT 1

这是我的 application_controller.rb:

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:firstname, :lastname, :username, :email, :password, :password_confirmation, :birthdate, :remember_me) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:firstname, :lastname, :email, :password, :password_confirmation, :current_password) }
end
end

这是我的 users.rb 模型:

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable, :timeoutable, :omniauthable,
:authentication_keys => [:login]
attr_accessor :login
validates :username, :presence => true, :uniqueness => true
validates :firstname, :presence => true
validates :lastname, :presence => true
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
end

我想我也正确设置了配置。

我不知道哪里出了问题。我尝试将 attr_accessor 移动到 devise :database_authenticatable, :registerable 上方,但这并没有真正帮助。

问题是我让这个在另一个应用程序上 100% 工作,它只是不工作! :(

感谢任何帮助!

最佳答案

已修复!

我改变了这部分:

def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end

def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end

self.find_for_database_authentication 更改为 self.find_first_by_auth_conditions

关于ruby-on-rails - 设计:尝试设置用户名字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24299278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com