用户的Rails NoMethodError但db:migrate没有帮助更新

时间:2013-04-26 15:58:10

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

我是rails的新手,我正在尝试在注册时添加电子邮件确认。我目前收到此错误。我尝试了rake db:migrate但由于某种原因我无法将email_activation_token导入用户表。我用sqllightbrowser检查过,它不存在。

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

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

用户#create 中的

NoMethodError
  

未定义的方法`email_activation_token'代表#(用户名:0xa2e96cc)

提取的来源(第3行):

  

3:= edit_email_activation_url(@ user.email_activation_token)

我试过这个无济于事

  

$ rails生成迁移add_email_activation_token_to_users

应用/分贝/迁移/(字符串)_add_email_activation_token_to_users.rb

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

应用/配置/ 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

应用/模型/ 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:rollback   rake db:migrate

但现在我的错误是不同的

  

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

2 个答案:

答案 0 :(得分:3)

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

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

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

运行rake db:migrate

确保要检查的数据库是运行迁移的数据库。

答案 1 :(得分:3)

我认为问题出现在rake db:migrate 运行rake db:migrate

后粘贴正在发生的事情

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