如何修改设计宝石?

时间:2014-01-30 15:43:07

标签: ruby-on-rails ruby ruby-on-rails-3 devise ruby-on-rails-4

由于基本设计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>

2 个答案:

答案 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行。这是因为该列不存在。

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