2016-12-01 78 views
0

我已经在网上跟踪文档,但是我仍然在挣扎,我不知道我错在哪里。当我尝试签署一个用户时,我所看到的只是'请在他们提交时回顾下面的问题',这个问题甚至没有被显示出来。终端也不多。实现用户名设计

应用控制器:

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    before_action :authenticate_user! 
    before_action :configure_permitted_parameters, if: :devise_controller? 

    protected 

    def configure_permitted_parameters 
    added_attrs = [:username, :email, :password, :password_confirmation, :remember_me] 
    devise_parameter_sanitizer.permit :sign_up, keys: [:username, :password] 
    devise_parameter_sanitizer.permit :account_update, keys: [:username, :password] 
    end 


end 

用户模型:

class User < ApplicationRecord 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    has_many :stories 
    validates :username, :presence => true, :uniqueness => { :case_sensitive => false} 
    validates_format_of :username, with: /^[a-zA-Z0-9_\.]*$/, :multiline => true 
    attr_accessor :login 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => {email: true, login: false} 

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

end 

在终端:

Processing by Devise::RegistrationsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"sTFYfOW5txc1AYinMyWDdaMfggAGh1oX/JSrR7vXc25cEwW5krezOQ6V5zE6QLXI6Dmwi8X3LN8s91rahJBxww==", "user"=>{"username"=>"indigo", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 
    (0.1ms) begin transaction 
    User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."username") = LOWER(?) LIMIT ? [["username", "indigo"], ["LIMIT", 1]] 
    (0.1ms) rollback transaction 
    Rendering users/registrations/new.html.erb within layouts/application 
    Rendered users/shared/_links.html.erb (1.3ms) 
    Rendered users/registrations/new.html.erb within layouts/application (8.7ms) 
/home/benjamin/Desktop/projectoxygen/app/views/layouts/application.html.erb:47: warning: else without rescue is useless 
Completed 200 OK in 306ms (Views: 42.4ms | ActiveRecord: 0.4ms) 

色器件分贝文件:

class DeviseCreateUsers < ActiveRecord::Migration[5.0] 
     def change 
     create_table :users do |t| 
      ## Database authenticatable 
      t.string :email,    null: false, default: "" 
      t.string :encrypted_password, null: false, default: "" 

      ## Recoverable 
      t.string :reset_password_token 
      t.datetime :reset_password_sent_at 

      ## Rememberable 
      t.datetime :remember_created_at 

      ## Trackable 
      t.integer :sign_in_count, default: 0, null: false 
      t.datetime :current_sign_in_at 
      t.datetime :last_sign_in_at 
      t.string :current_sign_in_ip 
      t.string :last_sign_in_ip 

      ## Confirmable 
      # t.string :confirmation_token 
      # t.datetime :confirmed_at 
      # t.datetime :confirmation_sent_at 
      # t.string :unconfirmed_email # Only if using reconfirmable 

      ## Lockable 
      # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 
      # t.string :unlock_token # Only if unlock strategy is :email or :both 
      # t.datetime :locked_at 


      t.timestamps null: false 
     end 

     add_index :users, :email,    unique: true 
     add_index :users, :reset_password_token, unique: true 

     change_column :users, :email, uniqueness: false 

     # add_index :users, :confirmation_token, unique: true 
     # add_index :users, :unlock_token,   unique: true 
     end 
    end 

当前错误:

ActiveRecord::RecordNotUnique in Devise::RegistrationsController#create 
SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("encrypted_password", "created_at", "updated_at", "username") VALUES (?, ?, ?, ?) 

终端:

ActiveRecord::RecordNotUnique in Devise::RegistrationsController#create 
SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("encrypted_password", "created_at", "updated_at", "username") VALUES (?, ?, ?, ?) 
+0

终端显示错误:'用户存在',尝试使用唯一的用户名! – dp7

+0

对不起,必须重做一次。我已经发布了上面的新终端错误。我在注册时输入了新的细节。 – Benjamints

+0

唯一用户名? – Benjamints

回答

1

如果您正在使用username代替email作为认证密钥,那么你必须定义:

#config/initializers/devise.rb 
config.authentication_keys = [:username] 

- OR,您可以在模型中定义身份验证密钥,如下所示:

devise :database_authenticatable, :authentication_keys => [:username] 

添加下面的方法,以避免验证电子邮件用户型号:

def email_required? 
    false 
end 

def email_changed? 
    false 
end 

如果你有在电子邮件列的唯一约束,然后创建一个迁移到电子邮件删除索引列:

def change 
    remove_index :users, :email 
end 
+0

谢谢,我添加了你的代码,错误消失了,但产生了一个新的。 SQLite3 :: ConstraintException:UNIQUE约束失败:users.email:INSERT INTO“users”(“encrypted_pa​​ssword”,“created_at”,“updated_at”,“username”)VALUES(?,?,?,?) – Benjamints

+0

ActiveRecord :: RecordNotUnique在Devise :: RegistrationsController#create – Benjamints

+0

我有:validatable,在我的user.rb中,它仍然出现了上面的错误。 – Benjamints