第一代后修改设备模块

发布于 2024-09-25 11:28:23 字数 938 浏览 0 评论 0原文

我正在学习rails。我发现 Devise 非常适合快速、无缝地启动和运行身份验证,但我确实有一个问题。

首次运行 Devise 生成器后如何更改模块(例如,rails g devise User)?默认情况下进行以下迁移:

def self.up
  create_table(:users) do |t|
    t.database_authenticatable :null => false
    t.recoverable
    t.rememberable
    t.trackable

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

    t.timestamps
  end

  add_index :users, :email,                :unique => true
  add_index :users, :reset_password_token, :unique => true
  # add_index :users, :confirmation_token,   :unique => true
  # add_index :users, :unlock_token,         :unique => true
end

如果我已经运行此迁移,我如何在稍后阶段添加/删除其中一些模块?例如,也许我想向现有的用户模型添加可锁定功能。我了解如何在模型和 devise.rb 中进行更改,但我不确定如何处理迁移。

抱歉,如果答案已经在这里,我已经在这里和谷歌中搜索了几个小时,但找不到任何东西。也许我正在寻找错误的东西。

预先感谢!
杰森
附:我正在使用
Rails 3.0.0
设计1.1.3

I'm in the process of learning rails. I've found Devise to be great with getting authentication up and running quickly and seamlessly but I do have one question.

How do I change the modules after the first run of the Devise generator (e.g. rails g devise User)? This defaults with the following migration:

def self.up
  create_table(:users) do |t|
    t.database_authenticatable :null => false
    t.recoverable
    t.rememberable
    t.trackable

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

    t.timestamps
  end

  add_index :users, :email,                :unique => true
  add_index :users, :reset_password_token, :unique => true
  # add_index :users, :confirmation_token,   :unique => true
  # add_index :users, :unlock_token,         :unique => true
end

If I've run this migration, how do I add/remove some of those modules at a later stage? E.g. Maybe I want to add lockable to an existing User model. I understand how to make the changes in the model and devise.rb but I'm not sure what to do with the migrations.

Apologies if the answer is here already, I've trawled for a couple of hours here and in google and couldn't find anything. Maybe I'm searching for the wrong thing.

Thanks in advance!
Jason
ps. I'm using
rails 3.0.0
devise 1.1.3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぶ宁プ宁ぶ 2024-10-02 11:28:23

我正在寻找同一问题的答案,幸运的是,碰巧坐在知道如何做的人旁边。

以下是通过迁移脚本更新用户模型以包含可确认模块的示例(使用 'railsgenerate migration add_confirmable_to_users' 生成的骨架脚本文件):

class AddConfirmableToUser < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.confirmable
    end
    add_index :users, :confirmation_token,   :unique => true
  end

  def self.down
    remove_column :users, :confirmable
    remove_index :users, :confirmation_token
  end
end

I was looking for answer to the same question, and luckily, happened to be sitting next to someone who knew how to do it.

Here is the example of updating the users model to include confirmable module through a migration script (the skeleton script file generated with 'rails generate migration add_confirmable_to_users'):

class AddConfirmableToUser < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.confirmable
    end
    add_index :users, :confirmation_token,   :unique => true
  end

  def self.down
    remove_column :users, :confirmable
    remove_index :users, :confirmation_token
  end
end
泪冰清 2024-10-02 11:28:23

我收到此错误:

undefined local variable or method `confirmed_at' for #<User:0x000001041531c8> (NameError)

要添加可确认 -

生成迁移:

$ rails generate migration add_confirmable_to_users

编辑迁移:

class AddConfirmableToUsers < ActiveRecord::Migration
  def change
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    add_column :users, :unconfirmed_email, :string
  end
end

http://guides.rubyonrails .org/migrations.html
https://github。 com/plataformatec/devise/wiki/操作方法:-升级到-Devise-2.0-migration-schema-style

I was getting this error:

undefined local variable or method `confirmed_at' for #<User:0x000001041531c8> (NameError)

To add Confirmable -

Generate the migration:

$ rails generate migration add_confirmable_to_users

Edit the migration:

class AddConfirmableToUsers < ActiveRecord::Migration
  def change
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    add_column :users, :unconfirmed_email, :string
  end
end

http://guides.rubyonrails.org/migrations.html
https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style

七婞 2024-10-02 11:28:23

只要您只是删除相应字段已添加到架构中的选项(例如可确认的字段),您始终可以直接编辑用户模型,而无需进行新的迁移。

As long as you're just removing options where the appropriate fields were already added to your schema (like confirmable), you can always just edit the Users model directly without a new migration.

明天过后 2024-10-02 11:28:23

更改迁移文件中所需的行,然后按照以下说明重做迁移:

http://guides。 rubyonrails.org/migrations.html

Change the lines you want in the migration file, then redo the migration as per these instructions:

http://guides.rubyonrails.org/migrations.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文