Vivek Dabhi
Vivek Dabhi

Reputation: 49

Remove email field in devise gem?

I would like to NOT require email for signing only mobile number to register user and login in using devise gem. I removed email from config/initializers/devise.rb:

Upvotes: 4

Views: 2443

Answers (3)

Montaguti
Montaguti

Reputation: 129

As a complement of @divyang's answers. You should also remove de email's index on users table...

remove_index "users", name: "index_users_on_email"

Upvotes: 1

Divyang Hirpara
Divyang Hirpara

Reputation: 998

This is the link to the validate.rb file of devise. You can see a method email_required? in the model. So I guess

 def email_required?
    false
 end

You need to put above method in your model.rb file.

You'll also need to make a slight modification to your users table. By default, Devise does not allow the email field to be null. Create and run change a migration that allows email to be null

 # in console
 rails g migration AddChangeColumnNullToUserEmail

 # migration file
class AddChangeColumnNullToUserEmail < ActiveRecord::Migration
  def self.up
    change_column :users, :email, :string, :null => true 
  end

  def self.down
    change_column :users, :email, :string, :null => false 
  end
end

Upvotes: 6

Shannon
Shannon

Reputation: 3018

You can use this guide, except use mobile instead of username. e.g.

In devise.rb:

config.authentication_keys = [:mobile]

In your controller:

.permit(:mobile)

Change the email field to your mobile field in app/views/devise/sessions/new.html.erb and app/views/devise/registrations/new.html.erb

In devise.en.yml:

invalid: 'Invalid mobile number or password.'
not_found_in_database: 'Invalid mobile number or password.'

Upvotes: 3

Related Questions