繁体   English   中英

如何修改设计宝石?

[英]How to modify the devise gem?

由于基本的设计宝石具有四个参数,因此我想通过email修改username

这是我试图做的

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>

您需要在config/initializers/devise.rb编辑config.authentication_keys行。 :email替换为:username

Devise Wiki中有更多信息- 如何:允许用户使用电子邮件地址以外的其他地址登录

根据您的步骤,您在更新迁移文件之前迁移了数据库。 如果是这种情况,则需要回滚迁移,然后再次运行。 您还需要在self.down中临时注释remove_column行。 这是因为该列不存在。

  1. 注释掉remove_column :hrs, :username, :string
  2. 运行bundle exec rake db:rollback
  3. 从步骤1取消注释行
  4. 运行bundle exec rake db:migrate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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