【问题标题】:NameError in Devise::RegistrationsController#create: undefined local variable or method `confirmed_at`Devise::RegistrationsController#create 中的 NameError:未定义的局部变量或方法 `confirmed_at`
【发布时间】:2019-06-24 21:40:02
【问题描述】:

在此处输入图片描述我是 Rails 新手,我正在尝试设计为用户获取身份验证电子邮件

# user.rb:          
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable,                                                      
  :validatable, :confirmable
end

# development.rb       
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { host: ENV['MAIL_HOST'] }  
config.action_mailer.delivery_method = :smtp      
config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: ENV['MAIL_HOST'],
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["GMAIL_USERNAME"],
  password: ENV["GMAIL_PASSWORD"] 
}
config.action_mailer.default_url_options = {:host => "localhost:3000"}

class AddConfirmableToDevise < ActiveRecord::Migration[5.2]
  def change
  end

  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
    User.update_all(:confirmed_at => Time.now)
    # All existing user accounts should be able to log in after this.
  end

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

我得到的错误是

未定义的局部变量或方法 `confirmed_at' for # 你的意思是?确认的? github 仓库是https://github.com/dinesh124/roughmart

【问题讨论】:

  • 当您尝试执行 rails db:migrate 时是否收到此错误?
  • 没有这个错误是当我尝试创建一个新用户时,它没有发送电子邮件进行身份验证
  • 你跑rails db:migrate了吗?
  • @rocky - 取消注释 # :confirmable, 并重新启动服务器。
  • 是的,在 rails db:migrate 之后仍然出现相同的错误,我正在尝试使用 :confirmable 对用户进行身份验证。还有其他方法吗?

标签: ruby-on-rails ruby email devise actionmailer


【解决方案1】:

您的列实际上并不存在于数据库中,因为您的迁移不起作用。

从您的迁移中删除def change,它会覆盖up/down

或者,在 change 方法中添加列并删除 up/down

【讨论】:

  • 您是否重新运行迁移然后重新启动服务器?
  • 是的,我也取消了确认部分的注释,配置了 smtp 服务器,我的架构也不匹配
【解决方案2】:

您需要的列不在数据库中。

您应该删除迁移中的更改方法。但是您不能更改迁移并再次运行它。它不会运行。

您应该回滚迁移,编辑迁移文件并再次运行它。

检查这个解释问题的答案:https://stackoverflow.com/a/10767930/3372172

【讨论】:

  • 是的!!!谢谢!!!!。它有帮助,但我仍然收到另一个错误:535 身份验证失败:用户名/密码错误。并在定义环境变量后出现无方法错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 2016-02-14
相关资源
最近更新 更多