好的,这没有意义。我可以向模型添加错误,但是当我调用有效时?或保存!它删除了我添加的错误。但是,常规验证将阻止模型被保存。为什么 errors.add 不起作用?
1.9.3p0 :001 > @order = Order.new(email: 'some@email.com')
=> #<Order id: nil, name: nil, address: nil, email: "some@email.com", pay_type: nil, created_at: nil, updated_at: nil, paypal_customer_token: nil, paypal_recurring_profile_token: nil, first_name: nil, last_name: nil, account_details: {}, unit_id_for_plan: nil, invoice: nil, purchased_at: nil>
1.9.3p0 :002 > @order.valid?
=> true
1.9.3p0 :003 > @order.errors.add :base, 'error'
=> ["error"]
1.9.3p0 :004 > @order.errors.size
=> 1
1.9.3p0 :005 > @order.valid?
=> true
1.9.3p0 :006 > @order.errors.size
=> 0
1.9.3p0 :007 > @order.errors.add :base, 'error_again'
=> ["error_again"]
1.9.3p0 :008 > @order.errors.size
=> 1
1.9.3p0 :009 > @order.save!
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO `orders` (`account_details`, `address`, `created_at`, `email`, `first_name`, `invoice`, `last_name`, `name`, `pay_type`, `paypal_customer_token`, `paypal_recurring_profile_token`, `purchased_at`, `unit_id_for_plan`, `updated_at`) VALUES ('--- {}\n', NULL, '2013-05-23 18:17:18', 'some@email.com', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2013-05-23 18:17:18')
(12.6ms) COMMIT
=> true
1.9.3p0 :010 > @order.errors.size
=> 0
Order 模型不包含太多验证:
attr_accessible :address, :email, :name, :pay_type, :paypal_customer_token, :paypal_payment_token, :first_name, :last_name, :account_details, :invoice
has_one :cart
has_many :line_items, :through => :cart
has_many :products, :through => :line_items, :source => :sellable, :source_type => 'Product'
has_many :airtime_plans, :through => :line_items, :source => :sellable, :source_type => 'AirtimePlan'
has_many :payment_notifications
serialize :account_details, Hash
validates_presence_of :email
validates_presence_of :email 正确会阻止保存工作,但是当我使用 errors.add :base 时,它不起作用。
@LeoCorrea 的回答让我想出了这个解决方案:
validate :user_validation, :on => :create
attr_accessor :users_invalid
def user_validation
if users_invalid
errors[:base] << "You have invalid user records"
end
end
#paypal_payment model in which the order is passed into
elsif resp["status"] == "error"
#@order.errors.add(:base, resp["body"])
@order.users_invalid = true
end
问题是我想要的实际错误消息在 resp[“body”] 中。现在我刚刚添加了一条无用的消息“您的用户记录无效”。