Rails 设计可确认的错误

Rails Devise confirmable error

目前正在尝试将确认邮件与设计身份验证集成。我遵循了设计文档中的说明:

https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users

当我尝试注册新用户时出现以下错误:

NameError in Devise::RegistrationsController#create
undefined local variable or method `confirmed_at' for #<User:0x9b87b38>

User.rb

  class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable



  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable
end

rails g迁移add_confirmable_to_devise

    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

confirmations_controller.rb

class ConfirmationsController < Devise::ConfirmationsController
  private
  def after_confirmation_path_for(resource_name, resource)
    your_new_after_confirmation_path
  end
end

routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers:{ confirmations: 'confirmations'}
  resources :videos
  get 'welcome/index'

  get 'welcome/new'

  root 'welcome#index'



end

我也看到有人说我应该添加 gem 'simple_token_authentication 到我的 Gemfile 和 运行

    rails g migration add_authentication_token_to_users authentication_token:string:index
rake db:migrate

但是,这并没有解决这个问题。

有什么想法吗?谢谢!

对于数据库中的每一列,ActiveRecord 都为 "set" 和 "get" 属性创建一个方法(对于 class,作为数据库中表示的列),名为在class栏之后。在这种情况下,class 是用户,列是 confirmed_at.

错误消息告诉您的是用户 class 上没有方法。您可以通过调用 User.methods.

查看方法

在没有看到您最新的 schema.rb 文件的情况下,我假设您缺少 Devise 可确认迁移。

创建一个新的迁移文件,rails g migration AddConfirmableToUsers;前往 db/migrations 并打开相应的迁移文件,然后 copy-paste this in:

class AddConfirmableToUsers < ActiveRecord::Migration def change change_table(:users) do |t| # Confirmable t.string :confirmation_token t.datetime :confirmed_at t.datetime :confirmation_sent_at t.string :unconfirmed_email # Only if using reconfirmable end add_index :users, :confirmation_token, :unique => true end end

然后调用 rake db:migrate.