I'm having an error on signup. Console indicated its an error "User exists" however i think its a problem with acts_as_paranoid conflicting with devise gem. Can you please help?
Gem - Device & ActsAsParanoid
It was working fine until i added acts_as_paranoid gem
Console response:
Started POST "/users" for 127.0.0.1 at 2018-09-18 20:25:17 +1000
Processing by Users::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WBPVa4QVwzij/j1H+6uOMNURddc2CQX/YJJ+pIKXi3mRwa4aIgOcYbwQKsPGO5sjFYUlC89lH1mn7SpmkYZ1qw==", "user"=>{"first_name"=>"Ben", "last_name"=>"Strachan", "email"=>"ben#ownerhealth.com.au", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
(0.2ms) BEGIN
User Exists (0.8ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND "users"."deleted_at" IS NULL LIMIT $2 [["email", "ben#ownerhealth.com.au"], ["LIMIT", 1]]
(0.2ms) ROLLBACK
Rendering devise/registrations/new.html.erb within layouts/auth
Rendered devise/shared/_links.html.erb (0.6ms)
Rendered devise/registrations/new.html.erb within layouts/auth (7.5ms)
(0.3ms) BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND "users"."deleted_at" IS NULL LIMIT $2 [["email", "ben#ownerhealth.com.au"], ["LIMIT", 1]]
(0.2ms) ROLLBACK
Completed 200 OK in 198ms (Views: 32.5ms | ActiveRecord: 2.2ms)
User model:
# == Schema Information
#
# Table name: users
#
# id :bigint(8) not null, primary key
# email :string default(""), not null
# encrypted_password :string default(""), not null
# reset_password_token :string
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string
# last_sign_in_ip :string
# created_at :datetime not null
# updated_at :datetime not null
# first_name :string
# last_name :string
# role :string
# invitation_token :string
# invitation_created_at :datetime
# invitation_sent_at :datetime
# invitation_accepted_at :datetime
# invitation_limit :integer
# invited_by_type :string
# invited_by_id :integer
# invitations_count :integer default(0)
# avatar_file_name :string
# avatar_content_type :string
# avatar_file_size :integer
# avatar_updated_at :datetime
# business_id :integer
# author_id :integer
# deleted_at :datetime
#
class User < ApplicationRecord
acts_as_paranoid
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
ROLES = [
ROLE_ADMIN = "Admin",
ROLE_REGULAR = "Regular"
]
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
validates :role, inclusion: ROLES, presence: true
has_attached_file :avatar, styles: { medium: "450x450>" },
default_url: -> (attachment) {
ActionController::Base.helpers.asset_path(
'default-avatar.png'
)
}
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
before_validation :set_role
belongs_to :business, optional: true
def full_name
[first_name, last_name].join(" ")
end
def admin?
self.role == ROLE_ADMIN
end
private
def set_role
self.role = ROLE_REGULAR if self.role.blank?
end
end
Registration controller:
class Users::RegistrationsController < Devise::RegistrationsController
layout 'auth'
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
# GET /resource/sign_up
# def new
# super
# end
# POST /resource
def create
super
if resource.save
business = Business.create first_name: resource.first_name,
last_name: resource.first_name,
email: resource.email
resource.update business_id: business.id
end
end
Schema:
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
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.string "first_name"
t.string "last_name"
t.string "role"
t.string "invitation_token"
t.datetime "invitation_created_at"
t.datetime "invitation_sent_at"
t.datetime "invitation_accepted_at"
t.integer "invitation_limit"
t.string "invited_by_type"
t.integer "invited_by_id"
t.integer "invitations_count", default: 0
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "business_id"
t.integer "author_id"
t.datetime "deleted_at"
t.index ["deleted_at"], name: "index_users_on_deleted_at"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
t.index ["invitations_count"], name: "index_users_on_invitations_count"
t.index ["invited_by_id"], name: "index_users_on_invited_by_id"
t.index ["invited_by_type", "invited_by_id"], name: "index_users_on_invited_by_type_and_invited_by_id"
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
I made an error! The user author_id relationship was wrong. This has fixed.
belongs_to :author, class_name: 'User', foreign_key: 'author_id', optional: true
Related
I'm creating a license server, but I stuck with problem, that Rails can't save model.
I set after_create method in User model, but got no luck, also I tried create License model with Rails console, but it rollback transaction and didn't show any error.
models/user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
authentication_keys: [:login]
attr_writer :login
has_one :license, dependent: :destroy
validates :username, presence: true, uniqueness: { case_sensitive: false }
validates_format_of :username, with: /^[a-zA-Z0-9_\.]*$/, multiline: true
after_create :create_assocs
def login
#login || self.username
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
if conditions[:username].nil?
where(conditions).first
else
where(username: conditions[:username]).first
end
end
end
def email_required?
false
end
private
def create_assocs
create_license(license_types_id: LicenseType.first.id)
# license = License.new(user_id: self.id, license_types_id: 1)
# license.save
# self.license.create(license_types_id: LicenseType.first.id)
end
end
models/license.rb
class License < ApplicationRecord
belongs_to :license_type
belongs_to :user
after_create :set_expired_at
private
def set_expired_at
# self.expired_at = DateTime.now + self.license_types.duration
end
end
in rails console,
2.5.1 :001 > license = License.new(license_types_id: LicenseType.first.id)
LicenseType Load (0.4ms) SELECT "license_types".* FROM "license_types" ORDER BY "license_types"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<License id: nil, expired_at: nil, created_at: nil, updated_at: nil, license_types_id: 1, user_id: nil>
2.5.1 :002 > license.save
(0.5ms) BEGIN
(0.2ms) ROLLBACK
=> false
schema.rb,
create_table "licenses", force: :cascade do |t|
t.datetime "expired_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "license_types_id"
t.bigint "user_id"
t.index ["license_types_id"], name: "index_licenses_on_license_types_id"
t.index ["user_id"], name: "index_licenses_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: ""
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.string "key"
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
add_foreign_key "licenses", "users"
What should I do to set the license for new user after creating?
License model contains two foreign key user_id and license_type_id
=> Which means before create License there must be a user who own this License as Rails 5 convention says user_id must exist
=> Also there must exist LicenseType as Rails 5 convention says license_type_id must exist
The rollback reasons can be investigated by following
license = License.new(license_types_id: LicenseType.first.id)
license.save
#Begin
#ROLLBACK
errors_stack = license.errors
errors_stack contains model level errors which causes rollback
To fix these rollback issue
user = User.first #or current_user
license = user.license.new(license_type_id: LicenseType.first.id)
license.save
Or
user = User.first #or current_user
license = License.new(license_type_id: LicenseType.first.id, user_id: user.id)
license.save
Or To create a User and assign the user a License # Alternative of after_create :create_assocs
new_user = User.new(...)
new_user.license.build(license_type_id: LicenseType.first.id)
new_user.save
Are you sure about this "no errors"? In Rails 5, belongs_to association is required by default, so I guess that's because it fails (you don't set user association prior to save attempt). So either you should set license.user, or set:
belongs_to :user, optional: true
in License model if your business logic doesn't require it.
I'm trying to add some extra information to my Devise User model like first_name, last_name, age, gender and city.
When I fill the signup form and click to submit I get this error:
SQLite3::ConstraintException: NOT NULL constraint failed: users.first_name: INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?)
def each
loop do
val = step
break self if done?
yield val
end
And these are the parameters if they are any use:
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"2e5oUwMw84HtwSuI09X1O5kjPLYk7SW4VKgGOOxcB93W7sSQYjPgq3N/BGo0+oAEifhec4lQ3PUt9vub17vs7g==",
"user"=>
{"first_name"=>"Test", "last_name"=>"Test", "age"=>"69", "city"=>"New York", "gender"=>"Trans", "email"=>"test.test#email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}
Here is my schema.rb just in case:
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
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.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.string "first_name", null: false
t.string "last_name", null: false
t.integer "age", null: false
t.string "city", null: false
t.string "gender", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Here is my user.rb model too:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
To have additional columns on user not already set in devise I allowed access in my ApplicationContoller this way. Rails4
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
# Only add some parameters
devise_parameter_sanitizer.for(:accept_invitation).concat [:first_name, :last_name]
end
or
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :full_name
end
and this would allow you to have first_name last_name with the other things that you have set up.
Rails5
def configure_permitted_parameters
additional_params = [:name, :company, :email_confirmation, {addresses_attributes: [:address1, :address2, :city, :state, :zip, :country, :name]}]
devise_parameter_sanitizer.permit(:sign_up, keys: additional_params)
devise_parameter_sanitizer.permit(:account_update, keys: additional_params)
end
You have a unique index on email. What the error is saying is that the email you are trying to save is not null in the database. A user already has this email. Change the email and it should work.
# to check for the user in the database
rails c
user = User.find_by_email("test.test#email.com") should bring back a user.
I have followed the documentation online, however I am still struggling and I don't know where I went wrong. When I am trying to sign a user up, all I am seeing is 'please review the problems below' upon them submitting, which isn't even being displayed.The terminal also isn't saying much.
Application Controller:
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
User model:
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
In the terminal:
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)
devise db file:
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
current errors:
ActiveRecord::RecordNotUnique in Devise::RegistrationsController#create
SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("encrypted_password", "created_at", "updated_at", "username") VALUES (?, ?, ?, ?)
terminal:
ActiveRecord::RecordNotUnique in Devise::RegistrationsController#create
SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("encrypted_password", "created_at", "updated_at", "username") VALUES (?, ?, ?, ?)
If you are using username instead of email as authentication key, then you must have defined:
#config/initializers/devise.rb
config.authentication_keys = [:username]
-
OR, you can define authentication key in model also as:
devise :database_authenticatable, :authentication_keys => [:username]
Add the following methods to model User in order to avoid validations for email:
def email_required?
false
end
def email_changed?
false
end
If you have a UNIQUE constraint over the email column, then create a migration to remove index on email column:
def change
remove_index :users, :email
end
I've attempted to create a has_many :through Association in my rails application, however I get an error when I do the following in the rails console.
2.0.0-p481 :001 > #group = Group.first
Group Load (0.2ms) SELECT "groups".* FROM "groups" ORDER BY "groups"."id" ASC LIMIT 1
=> #<Group id: 1, name: "First Group", description: "Woo", created_at: "2015-02-06 22:19:47", updated_at: "2015-02-06 22:19:47">
2.0.0-p481 :002 > #group.user
NoMethodError: undefined method `user' for #<Group:0x00000004d53eb0>
from /home/jon/.rvm/gems/ruby-2.0.0-p481/gems/activemodel-4.0.2/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /home/jon/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.2/lib/active_record/attribute_methods.rb:155:in `method_missing'
from (irb):2
from /home/jon/.rvm/gems/ruby-2.0.0-p481/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /home/jon/.rvm/gems/ruby-2.0.0-p481/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /home/jon/.rvm/gems/ruby-2.0.0-p481/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
My Models:
User:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
before_create :do_mailchimp
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :memberships
has_many :groups, through: :memberships
def do_mailchimp
gb = Gibbon::API.new("API")
gb.lists.subscribe({:id => 'ID',
:email => {:email => self.email },:double_optin => false})
end
end
Group:
class Group < ActiveRecord::Base
has_many :memberships
has_many :users, through: :memberships
end
Membership:
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
My Migrations:
DeviseCreateUsers:
class DeviseCreateUsers < ActiveRecord::Migration
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
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
CreateGroups:
class CreateGroups < ActiveRecord::Migration
def change
create_table :groups do |t|
t.string :name
t.string :description
t.timestamps
end
end
end
CreateMemberships:
class CreateMemberships < ActiveRecord::Migration
def change
create_table :memberships do |t|
t.timestamps
end
end
end
AddUserAndGroupIdToMemberships:
class AddUserAndGroupIdToMemberships < ActiveRecord::Migration
def change
add_column :memberships, :user_id, :integer
add_index :memberships, :user_id
add_column :memberships, :group_id, :integer
add_index :memberships, :group_id
end
end
My Schema:
ActiveRecord::Schema.define(version: 20150206225251) do
create_table "groups", force: true do |t|
t.string "name"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "memberships", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.integer "group_id"
end
add_index "memberships", ["group_id"], name: "index_memberships_on_group_id"
add_index "memberships", ["user_id"], name: "index_memberships_on_user_id"
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
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"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Thank you for your time hope you can help.
You defined that a Group has_many :users. Therefore a group does not have an user method, but an users method:
#group = Group.first
#group.users #=> returns an array with all users in the first group
#group.users.first #=> returns the first user in the first group
I'm absolutely new to Rails, I nearly do not know what I am doing. But. The problem is: signing up new user with Devise results in:
SQLite3::ConstraintException: column email is not unique:
INSERT INTO "users" ("created_at","encrypted_password", "name", "updated_at")
VALUES (?, ?, ?, ?)
And the request parameters:
{"utf8"=>"✓",
"authenticity_token"=>"1bgk4ovS3JitphVkIvcCZi3ex8QsBq4eEf6ZihQLiHg=",
"user"=>{"name"=>"Someone",
"email"=>"8#prosto.me",
"password"=>"[FILTERED]"},
"commit"=>"Sign up"}
User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable;
end
DB migration:
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
change_table(:users) do |t|
## Database authenticatable
t.string :email, :null => false, :default => ""
t.string :name, :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
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 # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
## Token authenticatable
# t.string :authentication_token
# Uncomment below if timestamps were not included in your original model.
# t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :name, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
def self.down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
end
end
Please tell me if I need to provide any other code.
And thank you for all your help in advance.
Update with DB schema:
ActiveRecord::Schema.define(version: 20131012114812) do
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
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"
t.datetime "updated_at"
t.string "name"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Update 2: And there is also a problem with authentication. Devise tells 'Invalid email or password' for any previously successfully signed up user in attempt to login.
SQLite is telling you that it's trying to create a new record, where one of the values will be an email, but there is already an existing record with that email.
An easy way to read your database records is query the DB in a terminal window:
$ rails console
$ User.all
If you want to see your test DB, which will be loaded with your fixtures:
$ rails console -e=test
$ User.all
Look for records that have the same email of the one you're trying to create.
If this is your first time using Devise, you should check your fixtures. Devise currently defaults to two fixtures that have no attributes. If you're running in a test environment then those fixtures will be loaded into the test DB as a two records with nil values for email, which are duplicate email values. Fixtures like the ones below will get you passed your error message.
file: app/test/fixtures/users.yml
one:
email: user#example.com
encrypted_password: password1
two:
email: user2#example.com
encrypted_password: password2
Do you have any other "email" columns in that database?
Perhaps you already had a "users" table, where the email column has been replicated with Devise. It would be helpful if you could show us which columns your table has :)
Try adding a uniqueness validation to your User model:
validates_uniqueness_of :email, :allow_blank => true
This will re-render your user creation form instead of causing an error.