Делаю регистрацию с devise
Добавил поле username для User. Для авторизации использую email + pwd.
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys:[:username])
end
end
class User < ApplicationRecord
validates :username, presence: true
devise :database_authenticatable,
:registerable,
:rememberable,
:trackable,
:validatable
end
ActiveRecord::Schema.define(version: 20170228132910) do
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "username", default: "", null: false
t.datetime "remember_created_at"
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"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index [nil], name: "index_users_on_reset_password_token", unique: true
end
end
Первый пользователь зарагистрировался нормально, на втором получаю ошибку
ActiveRecord::RecordNotUnique in Devise::RegistrationsController#create SQLite3::ConstraintException: UNIQUE constraint failed: index 'index_users_on_reset_password_token': INSERT INTO "users" ("email", "encrypted_password", "username", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
Что там ни так с index_users_on_reset_password_token ?
Добавил миграцию
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: ""
# Registrable
t.string :username, null: false, default: ""
## 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
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
end
end
t.index [nil]
-- што? Как это вышло? Покажите миграцию, это выдавшую.id
типаinteger
с автоинкрементом. Rails использует такой по умолчанию.reset_password_token