"Email cannot be empty." Come up with a username or email address.

I followed this guide on How to allow users to log in using my username or email address , and followed all the steps there, but when I try to register through the registrations/new.html.erb , I get this error:

 Email can't be blank 

In my model, I have:

 devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :username, :email, :password, :password_confirmation, :remember_me attr_accessor :login attr_accessible :login 

and

 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 

Any advice on this issue?

======= UPDATE

I found something here β–  [rails] How to use Devise and Rails without EMail here is something like:

  # Email is not required def email_required? false end 

With the addition in my model, I can create an entry with a username and leave the email field blank, but when I try to create a second entry without email, an error occurs in the database:

 Mysql2::Error: Duplicate entry '' for key 'index_parents_on_email':... 

Should I use this, remove the index from my table in the database and just check the username in the model? because I really don't need an email field on this model.

+6
source share
2 answers

Update

Asiniy answers below, works like a charm (:

Original (who knew?)

Your problem is the index in the mysql table, you can remove it from your migrations, but I'm not sure if this will solve your problem. It’s difficult not to use email in Devise, I would suggest a workaround as a fake email address from username , something like this

 "#{user.username}@fake.me" 

This is not very clean, but I see how omniauth comes up with something similar for passwords.

Rails 4 and strong options

I ran into this problem for username in rails 4 and I had to configure the allowed options as shown below for sign_up :

 class ApplicationController < ActionController::Base before_filter :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) do |u| u.permit :username, :email, :password, :password_confirmation end end end 

This is described in Devise Doc.

+12
source

Normal solution

 class User < AR::Base devise_for tralala def email_required? false end end 

Refer to https://github.com/plataformatec/devise/blob/master/lib/devise/models/validatable.rb#L29

+6
source

Source: https://habr.com/ru/post/921432/


All Articles