SNEH PANDYA
SNEH PANDYA

Reputation: 797

How to modify the devise gem?

As the basic devise gem has the four parameters,i want to modify username with email

Here is what i tried to do

step-1 - rails g migration add_username_hrs username:string

step-2 - bundle exec rake db:migrate

step-3 - updated add_username_hrs.rb

class AddUsernameHrs < ActiveRecord::Migration
    def self.up
         add_column :hrs, :username, :string
    end
    def self.down
         remove_column :hrs, :username, :string
    end
 end

step-4 - Replace **:email** with **:username**

<%= f.label :username %><br /> 
<%= f.text_field :username, :autofocus => true , :placeholder => "Username" %>

Error

undefined method `username' for #<Hr:0x2951c88>

Upvotes: 0

Views: 147

Answers (2)

msergeant
msergeant

Reputation: 4801

According to your steps, you migrated your database before updating the migration file. If that is the case, you'll need to rollback your migration and then run it again. You will also want to temporarily comment out the remove_column line in self.down. This is because that column does not exist.

  1. Comment out remove_column :hrs, :username, :string
  2. run bundle exec rake db:rollback
  3. Uncomment line from step 1
  4. run bundle exec rake db:migrate

Upvotes: 0

user2062950
user2062950

Reputation:

You'll need to edit the config.authentication_keys line in config/initializers/devise.rb. Replace the :email with :username.

There's more info in the Devise wiki - How To: Allow users to sign in with something other than their email address

Upvotes: 1

Related Questions