25

テストを実行すると、このエラーが発生します。すべての s のスペルが正しいことを確認しましたemail_confirmation(私が頭がおかしくない限り)。私は少し Rails 初心者なので、簡単なことかもしれません。

ユーザーモデル

class User < ActiveRecord::Base
  attr_accessible :email, :email_confirmation, :first_name, :last_name,
                  :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }

  validates :first_name, presence: true, length: { maximum: 25 }
  validates :last_name, presence: true, length: { maximum: 25 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :email_confirmation, presence: true
  validates :password, presence: true, length: { maximum: 6 }
  validates :password_confirmation, presence: true
end

Rspec テスト

require 'spec_helper'

describe User do
  before { @user = User.new(email: "user@example.com",
                            first_name: "John", last_name: "Smith",
                            password: "foobar", password_confirmation: "foobar",
                            email_confirmation: "user@example.com") }

  subject { @user }

  it { should respond_to(:first_name) }
  it { should respond_to(:last_name) }
  it { should respond_to(:email) }
  it { should respond_to(:email_confirmation) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:authenticate) }

  it { should be_valid }

  describe "when first name is not present" do
    before { @user.first_name = " " }
    it { should_not be_valid }
  end

  describe "when last name is not present" do
    before { @user.last_name = " " }
    it { should_not be_valid }
  end

  describe "when email is not present" do
    before { @user.email = @user.email_confirmation = " " }
    it { should_not be_valid }
  end

  describe "when password is not present" do
    before { @user.password = @user.password_confirmation = " " }
    it { should_not be_valid }
  end

  describe "when first_name is too long" do
    before { @user.first_name = "a" * 26 }
    it { should_not be_valid }
  end

  describe "when last_name is too long" do
    before { @user.last_name = "a" * 26 }
    it { should_not be_valid }
  end

  describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.
                             foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not be_valid
     end      
    end
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end      
    end
  end

  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

    it { should_not be_valid }
  end

  describe "when password doesn't match confirmation" do
    before { @user.password_confirmation = "mismatch" }
    it { should_not be_valid }
  end

  describe "when email doesn't match confirmation" do
    before { @user.email_confirmation = "mismatch@example.com" }
    it { should_not be_valid }
  end

  describe "when password confirmation is nil" do
    before { @user.password_confirmation = nil }
    it { should_not be_valid }
  end

  describe "when email confirmation is nil" do
    before { @user.email_confirmation = nil }
    it { should_not be_valid }
  end

  describe "with a password that's too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
  end

  describe "return value of authenticate method" do
    before { @user.save }
    let(:found_user) { User.find_by_email(@user.email) }

    describe "with valid password" do
      it { should == found_user.authenticate(@user.password) }
    end

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not == user_for_invalid_password }
      specify { user_for_invalid_password.should be_false }
    end
  end
end

schema.rb

ActiveRecord::Schema.define(:version => 20130417021135) do

  create_table "users", :force => true do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "email"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
    t.string   "password_digest"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true

end
4

6 に答える 6

27

これの私自身のインスタンスをデバッグするのにかなりの時間を費やしたばかりなので、3 番目の可能性を考えてみようと思いました。

移行を正しく行いActiveRecord、Rails コンソールで調べて確認しました。スキーマからデータベースを何度も再作成しようとしましたが、移行を何度も再実行しようとしましたが、すべて役に立ちませんでした。

私の場合の問題は、実行時ではなく、単体テストの実行時に問題が発生したことです。問題は、移行/ロールバック テストでテスト データベースが同期していないことです。解決策は非常に簡単でした。私がしなければならなかったのは、テストデータベースを次のようにリセットすることだけでした:

rake db:test:prepare
于 2016-09-09T13:39:30.893 に答える
11

上記の回答が正しいとマークされ、OPの問題が解決されることを理解しています。しかし、このトピックに関する多くのスタックオーバーフローの投稿では無視されている、このエラーの別の原因があります。このエラーは、has_many に as: オプションを使用するのを忘れた場合に、ポリモーフィック多対多で発生する可能性があります。例えば:

class AProfile < ActiveRecord::Base
  has_many :profile_students
  has_many :students, through: :profile_students
end

class BProfile < ActiveRecord::Base
  has_many :profile_students
  has_many :students, through: :profile_students
end

class ProfileStudent < ActiveRecord::Base
  belongs_to :profile, polymorphic: :true
  belongs_to :student
end

class Student < ActiveRecord::Base
  has_many :profile_students
  has_many :aprofiles, through: :profile_students
  has_many :bprofiles, through: :profile_students
end

これにより、次のエラーが発生します。

Getting “ActiveRecord::UnknownAttributeError: unknown attribute: profile_id

次のことをしようとすると:

a = AProfile.new
a.students << Student.new

解決策は、:as オプションを AProfile と BProfile に追加することです。

class AProfile < ActiveRecord::Base
  has_many :profile_students, as: :profile
  has_many :students, through: :profile_students
end

class BProfile < ActiveRecord::Base
  has_many :profile_students, as: :profile
  has_many :students, through: :profile_students
end
于 2014-12-15T23:45:56.103 に答える
-1

同じメッセージ エラーが発生し、データベース内の列定義と同じ順序でパラメーターの順序を修正しました。

コントローラ

def create
    worktime = Worktime.create(name: params[:name], workhours: params[:workhours], organization: @organization, workdays: params[:workdays])

    render json: worktime
end

データベース

Table: worktimes
Columns:
id  int(11) AI PK
name    varchar(255)
workhours   text
organization_id int(11)
workdays    text
于 2015-07-29T13:52:59.127 に答える