在非设计用户登录设置上实现设计可恢复和可确认

时间:2014-08-25 15:50:06

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

我为用户登录和oauth登录开发了自定义功能,现在我想添加忘记密码选项(重置用户密码)并确认电子邮件(包括发送确认电子邮件)。

是否可以在不重建我的整个用户模型和会话控制器的情况下添加这些设计功能?

1 个答案:

答案 0 :(得分:0)

简短回答:Yes it's possible

更长的答案:

以下是如何向用户介绍确认信息,同时将现有用户标记为已确认。

将此添加到您的用户模型。

devise :registerable, **:confirmable**

然后,进行迁移: (在你的终端)

rails g migration add_confirmable_to_devise

您的新迁移:

class AddConfirmableToDevise < ActiveRecord::Migration
  # Note: You can't use change, as User.update_all with fail in the down migration
  def self.up
    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 # Only if using reconfirmable
    add_index :users, :confirmation_token, :unique => true
    # User.reset_column_information # Need for some types of updates, but not for update_all.
    # To avoid a short time window between running the migration and updating all existing
    # users as confirmed, do the following
    User.update_all(:confirmed_at => Time.now)
    # All existing user accounts should be able to log in after this.
  end

  def self.down
    remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
    # remove_columns :users, :unconfirmed_email # Only if using reconfirmable
  end
end

如果不使用可重新配置,请更新config / initializers / devise.rb中的配置

config.reconfirmable = false

在您的终端中:

rails generate devise:views confirmable
rake db:migrate

注意:

  1. 我有同样的问题并以同样的方式做到了。我只是从源头复制粘贴。
  2. 如果您有任何进一步的问题,请查看您的第一个来源should be the devise How to page with over 9000 tutorials