Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 设计:尝试设置用户名字段_Ruby On Rails_Postgresql_Devise - Fatal编程技术网

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

Ruby on rails 设计:尝试设置用户名字段,ruby-on-rails,postgresql,devise,Ruby On Rails,Postgresql,Devise,我正在尝试使用以下教程设置Desive的用户名字段: 登录和其他操作时,“登录”字段工作正常,但当我尝试重置密码或重新发送确认消息时,postgres出现错误: PG::UndefinedColumn: ERROR: column users.login does not exist LINE 1: SELECT "users".* FROM "users" WHERE "users"."login" = 'nah... ^ : SELECT "users".* FROM "users

我正在尝试使用以下教程设置Desive的用户名字段:

登录和其他操作时,“登录”字段工作正常,但当我尝试重置密码或重新发送确认消息时,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
这是我的应用程序_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
class ApplicationController
这是我的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
class用户[:登录]
属性访问器:登录
验证:username,:presence=>true,:university=>true
验证:firstname,:presence=>true
验证:lastname,:presence=>true
定义self.find_用于_数据库_身份验证(典狱长条件)
条件=典狱长_conditions.dup
如果login=conditions.delete(:login)
where(条件)。where([“lower(username)=:value或lower(email)=:value”,{:value=>login.downcase}])
其他的
在哪里(条件)。首先
结束
结束
结束
我想我也正确地设置了配置

我不知道怎么了。我尝试将attr\u访问器移到
designe:database\u authenticable,:registerable
上方,但这并没有真正起到作用

问题是,我在另一个应用程序上得到了100%的效果,但它不适用于此!:(

感谢您的帮助!

它说您需要将以下内容添加到您的
config/initializers/designe.rb
文件中,以“将designe配置为使用登录作为重置密码或确认密钥”:

还要确保更新您的视图。在
密码/new.html.erb
删除:

<p><%= f.label :email %><br />
<%= f.text_field :email %></p>
并加上:

<p><%= f.label :login %><br />
<%= f.text_field :login %></p>
<p><%= f.label :login %><br />
<%= f.text_field :login %></p>

修复了它

我改变了这部分:

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


更改
self.find_for_database_authentication
self.find_first_by_auth_conditions

我需要检查,但是你是否重新启动了
rails服务器
?初始化程序只在rails服务器启动时运行。不,我多次重新启动服务器。嗯。你有多个型号使用designe吗?rails的版本是什么你在使用?不,只有一个模型(用户)。我正在运行4.1.1。这很奇怪。你能尝试将
重置密码密钥
确认密钥
移动到你的
用户
模型中,方法与你的
:验证密钥=>[:登录]相同吗
?我尝试了你的答案,但同样的结果我也在使用active admin。有什么帮助吗?
<p><%= f.label :login %><br />
<%= f.text_field :login %></p>
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