jos*_*ton 19 validation ruby-on-rails
我在运行测试时遇到此错误.我已经检查过以确保所有email_confirmation
s拼写正确并且(除非我疯了)他们是.我有点像Rails noob,所以它可能很简单.
用户模型
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Stu*_*t M 25
你得到的UnknownAttributeError
是因为你的users
表中没有一个列email_confirmation
.默认情况下,ActiveRecord将查找与用于构造模型的属性相同的DB列,但此行尝试使用数据库不知道的属性构造User:
before { @user = User.new(email: "user@example.com",
first_name: "John", last_name: "Smith",
password: "foobar", password_confirmation: "foobar",
email_confirmation: "user@example.com") }
Run Code Online (Sandbox Code Playgroud)
您真的打算在数据库中保存电子邮件确认,还是只想在保存之前检查它是否与电子邮件匹配?我假设后者,Rails实际上内置支持这样做:
class User < ActiveRecord::Base
validates :email, :confirmation => true
validates :email_confirmation, :presence => true
end
Run Code Online (Sandbox Code Playgroud)
请参阅Rails指南验证或validates_confirmation_of
API文档的更多详细信息.(你可能需要做同样的事情:password_confirmation
.)
aga*_*her 19
刚花了大量时间调试我自己的实例,我想我会以第三种可能性进行调整.
我已经正确完成了迁移并通过检查我ActiveRecord
在rails控制台中验证了它.我曾尝试多次从架构中重新创建我的数据库,并尝试多次重新运行迁移,但都无济于事.
在我的例子中,问题是我在运行单元测试时看到了问题,而不是在运行时.问题是我的测试数据库在我的迁移/回滚测试中已经不同步了.解决方案非常简单.我所要做的就是重置测试数据库:
rake db:test:prepare
Run Code Online (Sandbox Code Playgroud)
Don*_*ato 11
我知道上面的答案标记正确并解决了OP的问题.但是,在此主题的许多stackoverflow帖子中都没有注意到此错误的另一个原因.当您忘记使用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
Run Code Online (Sandbox Code Playgroud)
这会给你这个错误:
Getting “ActiveRecord::UnknownAttributeError: unknown attribute: profile_id
Run Code Online (Sandbox Code Playgroud)
当您尝试执行以下操作时:
a = AProfile.new
a.students << Student.new
Run Code Online (Sandbox Code Playgroud)
解决方案是在AProfile和BProfile中添加:as选项:
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
Run Code Online (Sandbox Code Playgroud)
小智 5
我有同样的问题,这就像魔法一样。在您要更新的每个模型的迁移语句的末尾添加此行。重置有关列的所有缓存信息,这将导致它们在下一个请求时重新加载。
<ModelName>.reset_column_information
Run Code Online (Sandbox Code Playgroud)
参考:https : //apidock.com/rails/ActiveRecord/Base/reset_column_information/class
归档时间: |
|
查看次数: |
46723 次 |
最近记录: |