Rails pg数据库迁移出现undefined method 'database_authenticatable' for Devise Users错误

3

undefined method database_authenticatable' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0xd715388>

这个错误是由于在数据库迁移文件中调用了未定义的方法database_authenticatable导致的。

The migration is:

这个迁移文件内容如下:
class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable
      t.timestamps
    end
    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
  end
  def self.down
    drop_table :users
  end
end

你使用的 devise 版本是什么? - Lee Jarvis
https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0 - Lee Jarvis
2个回答

9
如果我没记错的话,Devise 更改了其生成的迁移样式,从而使原先的 '<' 开始的表达式变成了“t.”。
create_table(:user) do |t|
  t.database_authenticatable
end

to

create_table(:user) do |t|
  ## Database authenticatable
  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""
end

在 2.0 版本之后。

更新:请参阅此wiki


+1 有用。但是如果这是一个已经存在的Rails应用程序并且用户迁移是早期的之一,那么正确的方法是什么?我应该编辑它吗?删除它并重新开始一个全新的?做一个迁移来删除旧的并添加新的?对这些问题的正确回答将帮助指导我找到一个好的解决方案。 - Michael Durrant
我查看了上述维基参考 - https://github.com/plataformatec/devise/wiki/How-To%3a-Upgrade-to-Devise-2.0-migration-schema-style - 它很有帮助,例如,“您需要更改原始的 Devise 迁移以不使用 Before 助手,而是使用列出的字段...” - Michael Durrant
也许将维基更新链接放在帖子顶部而不是底部会更好? - handler

0
这对我有效:
create_table(:users) do |t|
  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""
  ## Recoverable
  t.string   :reset_password_token
  t.datetime :reset_password_sent_at

  ## Rememberable
  t.datetime :remember_created_at

  ## Trackable
  t.integer  :sign_in_count, :default => 0
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.string   :current_sign_in_ip
  t.string   :last_sign_in_ip

  # t.encryptable
  # t.confirmable
  # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
  # t.token_authenticatable


  t.timestamps
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接