由于基本设计gem有四个参数,我想用username
修改email
以下是我尝试做的事情
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" %>
错误
undefined method `username' for #<Hr:0x2951c88>
答案 0 :(得分:1)
您需要修改config.authentication_keys
中的config/initializers/devise.rb
行。将:email
替换为:username
。
Devise wiki中有更多信息 - How To: Allow users to sign in with something other than their email address
答案 1 :(得分:0)
根据您的步骤,您在更新迁移文件之前迁移了数据库。如果是这种情况,您将需要回滚迁移,然后再次运行它。您还需要暂时注释掉self.down中的remove_column行。这是因为该列不存在。
remove_column :hrs, :username, :string
bundle exec rake db:rollback
bundle exec rake db:migrate