【问题标题】:Rails NoMethodError for user but db:migrate isn't helping -updatedRails NoMethodError 用户但 db:migrate 没有帮助-更新
【发布时间】:2013-04-26 15:58:10
【问题描述】:

我是 Rails 新手,我正在尝试在注册时添加电子邮件确认。我目前收到此错误。我尝试了 rake db:migrate 但由于某种原因我无法将 email_activation_token 放入用户表中。我用 sqllightbrowser 查了一下,没有。

我正在使用 Haml,如下所示。

奖励积分:如果您告诉我如何将 email_activation_token 设置为布尔值并使其默认为 false。

Users#create 中的 NoMethodError

#(User:0xa2e96cc) 的未定义方法 `email_activation_token'

提取的源代码(第 3 行附近):

3: = edit_email_activation_url(@user.email_activation_token)

我试过没有用

$ rails 生成迁移 add_email_activation_token_to_users

app/db/migrate/(string)_add_email_activation_token_to_users.rb

class AddEmailActivationTokenToUsers < ActiveRecord::Migration
  def change
   add_column :users, :email_activation_token, :string
  end
end

app/config/routes.rb

SomeApp::Application.routes.draw do
 get "password_resets/new"
 get "sessions/new"
 resources :users
 resources :sessions
 resources :password_resets
 get "static_pages/home"
 get "static_pages/help"
 root to: 'static_pages#home'
 match "sign_up",  to: "users#new"
 match '/help',    to: 'static_pages#help'
 match '/log_in',  to: 'sessions#new'
 match '/log_out', to: 'sessions#destroy'
end

app/models/user.rb

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation

  attr_accessor :password
  before_save :encrypt_password
  before_save { |user| user.email = email.downcase }
  before_create { generate_token(:auth_token) }

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  VALID_PASSWORD_REGEX = /^(?=.*[a-zA-Z])(?=.*[0-9]).{6,}$/
  validates_confirmation_of :password
  validates :password, :on => :create, presence: true, format: { with: VALID_PASSWORD_REGEX }
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

编辑更新

我通过

在数据库中得到它

rake db:回滚 耙分贝:迁移

但现在我的错误不同了

#(#(Class:0xacca7f4):0xa614ef0) 的未定义方法 `edit_email_activation_url'

【问题讨论】:

  • “rake db:migrate”和“cat db/schema.rb”的输出是什么?
  • 对不起?那还不清楚。你的意思是我对用户邮件密码重置的看法是什么?
  • 如果你去你的终端运行上面的命令,你会得到什么输出/文本?可以贴在这里吗?

标签: ruby-on-rails ruby-on-rails-3 authentication haml


【解决方案1】:

运行 rake db:migrate:status 并检查相关迁移是否已运行。

如果已经运行,请执行rake db:rollback,这将回滚上次迁移。

add_column :users, :email_activation_token, :boolean, :default => false

运行rake db:migrate

确保您正在检查的数据库是正在运行迁移的数据库。

【讨论】:

  • 谢谢。你的绅士和学者。
  • 好的,至少它已添加到数据库中,但现在我收到此错误 undefined method `edit_email_activation_url' for #:0xa614ef0>
【解决方案2】:

我认为问题出在你rake db:migrate 粘贴运行rake db:migrate后发生的事情

检查您的数据库中的 schema_migrations 表,并检查您的书面迁移是否存在时间戳。 (time-stamp)_add_email_activation_token_to_users.rb.

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多