Rails NoMethodError for user but db:migrate 没有帮助 - 更新



我是rails的新手,我试图在注册时添加电子邮件确认。我目前得到这个错误。我尝试了rake db:migrate,但由于某种原因,我无法获得email_activation_token到用户表中。我检查了sqllightbrowser,它不在那里。

我使用的是Haml,如下图所示。

奖励点:如果你告诉我如何设置email_activation_token为布尔值,并使其默认为false。

noomethoderror in Users#create

未定义的方法' email_activation_token'为#(用户:0xa2e96cc)

提取的源代码(第3行左右):

3: = edit_email_activation_url(@user.email_activation_token)

I tried this to no avail

$ rails generate migration add_email_activation_token_to_users

应用程序/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

应用程序/模型/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-zd-.]+.[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:回滚

但现在我的错误是不同的

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

运行rake db:migrate:status,检查所讨论的迁移是否已经运行。

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

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

执行命令rake db:migrate .

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

我认为问题出在你的rake db:migrate上粘贴运行rake db:migrate后的结果

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

相关内容

最新更新