简体   繁体   中英

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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM