我正在使用Devise 4.2.1运行Rails 5.1。
我已配置Devise确认: class User<的ActiveRecord :: Base的 设计:database_authenticatable,:registerable,:确认, :可恢复的,:可记住的,:可跟踪的,:有效的
我已设置路线: devise_for:users
我已生成视图,并自定义注册和确认。
我也生成了控制器,但没有自定义控制器,也没有在路由中引用它们。
我在App Controller中设置了certndicate用户: class ApplicationController<的ActionController :: Base的 #Login用户 before_action:authenticate_user!
当用户注册时,他被重定向到登录而不是确认/ new.html.erb
我做错了什么?
答案 0 :(得分:1)
按照以下链接中正确提及的所有步骤进行操作:
https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users
首先,将设计:confirmable
添加到您的models/user.rb
文件
然后,执行迁移:
rails g migration add_confirmable_to_devise
将生成db/migrate/YYYYMMDDxxx_add_confirmable_to_devise.rb.
添加以下内容以进行迁移。
class AddConfirmableToDevise < ActiveRecord::Migration
# Note: You can't use change, as User.update_all will fail in the down migration
def 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
execute("UPDATE users SET confirmed_at = NOW()")
# All existing user accounts should be able to log in after this.
# Remind: Rails using SQLite as default. And SQLite has no such function :NOW.
# Use :date('now') instead of :NOW when using SQLite.
# => execute("UPDATE users SET confirmed_at = date('now')")
# Or => User.all.update_all confirmed_at: Time.now
end
def down
remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
# remove_columns :users, :unconfirmed_email # Only if using reconfirmable
end
end
如果尚未创建相应的Devise视图,也可以生成它们:
rails generate devise:views users
执行迁移rake db:migrate
重启服务器。