0
我的訂單類中有這個。我想驗證shipping_address
的存在,除非它與billing_address
相同。然而,我的規格仍然失敗。驗證送貨地址的存在,除非它與帳單地址相同
class Order < ActiveRecord::Base
attr_writer :ship_to_billing_address
belongs_to :billing_address, class_name: 'Address'
belongs_to :shipping_address, class_name: 'Address'
accepts_nested_attributes_for :billing_address, :shipping_address
validates :shipping_address, presence: true, unless: -> { self.ship_to_billing_address? }
def ship_to_billing_address
@ship_to_billing_address ||= true
end
def ship_to_billing_address?
self.ship_to_billing_address
end
end
但我不斷收到失敗的規格(例如預計爲無效):
describe "shipping_address_id" do
context "when shipping address is different from billing address" do
before { @order.ship_to_billing_address = false }
it_behaves_like 'a foreign key', :shipping_address_id
end
end
shared_examples 'a foreign key' do |key|
it "can't be nil, blank, or not an int" do
[nil, "", " ", "a", 1.1].each do |value|
@order.send("#{key}=", value)
@order.should_not be_valid
end
end
end
表單代碼:
= f.check_box :ship_to_billing_address
| Use my shipping address as my billing address.
謝謝。它的核心是我想將'ship_to_billing_address'初始化爲'true',然後根據表單進行更改。這是做這件事的最好方法嗎? – Mohamad 2013-05-08 19:31:55
查看我的更新。 – DNNX 2013-05-08 19:32:44
謝謝。我的規格現在已經過去了。但是儘管複選框被選中,該表單仍然顯示帳單地址的錯誤!任何想法爲什麼?我添加了表單代碼。 – Mohamad 2013-05-08 19:38:47