1

在控制台中运行测试后出现以下错误:

ActiveRecord::StatementInvalid: SQLite3::SQLException: table users has no column named password: INSERT INTO "users"

用户测试.rb:

class UserTest < ActiveSupport::TestCase
  test "a user should enter a first name" do
    user = User.new
    assert !user.save
    assert !user.errors[:first_name].empty?
  end

  test "a user should enter a last name" do
    user = User.new
    assert !user.save
    assert !user.errors[:last_name].empty?
  end

  test "a user should enter a profile name" do
    user = User.new
    assert !user.save
    assert !user.errors[:profile_name].empty?
  end

  test "a user should have a unique profile name" do
    user = User.new
    user.profile_name = users(:adam).profile_name

    assert !user.save
    assert !user.errors[:profile_name].empty?
  end
end

用户.rb:

 class User < ActiveRecord::Base
 # Include default devise modules. Others available are:
 # :token_authenticatable, :confirmable,
 # :lockable, :timeoutable and :omniauthable
 devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
        :first_name, :last_name, :profile_name

validates :first_name, presence: true

validates :last_name, presence: true

validates :profile_name, presence: true,
        uniqueness: true

has_many :statuses

def full_name
first_name + " " + last_name
end
end

用户.yml:

dan:
  first_name: "Dan"
  last_name: "Can"
  email: "dan@email.com"
  profile_name: "dan"
  password: "123456"
  password_confirmation: "123456"

数据库.yml:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
 adapter: sqlite3
 database: db/development.sqlite3
 pool: 5
 timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

我认为是我的用户迁移文件:

class DeviseCreateUsers < ActiveRecord::Migration
def change
  create_table(:users) do |t|
  t.string :first_name
  t.string :last_name
  t.string :profile_name

  ## Database authenticatable
  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""

  ## Recoverable
  t.string   :reset_password_token
  t.datetime :reset_password_sent_at

  ## Rememberable
  t.datetime :remember_created_at

  ## Trackable
  t.integer  :sign_in_count, :default => 0
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.string   :current_sign_in_ip
  t.string   :last_sign_in_ip

  ## Confirmable
  # t.string   :confirmation_token
  # t.datetime :confirmed_at
  # t.datetime :confirmation_sent_at
  # t.string   :unconfirmed_email # Only if using reconfirmable

  ## Lockable
  # t.integer  :failed_attempts, :default => 0 # Only if lock strategy is     :failed_attempts
  # t.string   :unlock_token # Only if unlock strategy is :email or :both
  # t.datetime :locked_at

  ## Token authenticatable
  # t.string :authentication_token


  t.timestamps
end

 add_index :users, :email,                :unique => true
 add_index :users, :reset_password_token, :unique => true
end
end

我想知道是什么导致了错误,但更重要的是为什么。

4

3 回答 3

4

当我从用户夹具中删除passwordandpassword_confirmation列时,它通过了测试,没有错误。一位朋友告诉我,这可能是由于设计升级所致。

于 2013-04-30T01:20:24.647 回答
0

我认为这里有几种可能性,但我将专注于眼前的问题。如果您的 Rake 任务正在返回No command found,则可能是因为您的计算机上未安装 Rake。我会从那里开始。

要安装 Rake,请在终端中输入:

gem install rake

您的代码不起作用的原因是您的 users 表没有名为password. 使用rake db:migrate并且rake db:test:prepare您确保您创建的任何迁移都应用于您的数据库。

让我知道结果。

于 2013-04-29T03:31:25.960 回答
0

在我看来,您遇到的问题是,正如错误所说,您的用户表中没有密码列。您必须将其添加到您的迁移中:

t.string   :password

但是请注意,我从未使用过 Devise,所以我可能错了。

于 2013-04-29T04:00:08.413 回答