3
我正在使用改革創業板在我的網上商店做結帳的表單對象。我有Checkout表單,其中包含Order模型的屬性,它與Address模型有關聯。改革創業板:使用多種形式的一種模式
問題是訂單模型具有相同的地址模型中的兩個聯想:
class Order < ActiveRecord::Base
#...
belongs_to :billing_address, :class_name => 'Address'
belongs_to :shipping_address, :class_name => 'Address'
#...
end
所以我需要建立自己的Google Checkout形式使用的地址模式的兩倍。我曾經申請的臨時方法很簡單。但它工作正常:
class Checkout < Reform::Form
extend ::ActiveModel::Callbacks
#...
property :billing_address, populate_if_empty: Address do
property :firstname
property :lastname
property :address1
property :address2
property :phone
property :city
property :zipcode
property :country_id
property :billing_address_for_id
property :shipping_address_for_id
validates :firstname,
:lastname,
:address1,
:phone,
:city,
:zipcode,
:country_id,
presence: true
# provided by phony_rails gem
# validates phone number to be correct and plausible
# without country accordance
validates :phone, phony_plausible: { ignore_record_country_code: true }
# provided by validates_zipcode gem
# validates zipcode to be correct due to country alpha2 code
validates :zipcode, zipcode: { country_code: :country_code }
end
property :shipping_address, populate_if_empty: Address do
property :firstname
property :lastname
property :address1
property :address2
property :phone
property :city
property :zipcode
property :country_id
property :billing_address_for_id
property :shipping_address_for_id
validates :firstname,
:lastname,
:address1,
:phone,
:city,
:zipcode,
:country_id,
presence: true
# provided by phony_rails gem
# validates phone number to be correct and plausible
# without country accordance
validates :phone, phony_plausible: { ignore_record_country_code: true }
# provided by validates_zipcode gem
# validates zipcode to be correct due to country alpha2 code
validates :zipcode, zipcode: { country_code: :country_code }
#...
end
但很明顯,重複的代碼必須重構。在那裏,我發現我不能提出任何工作解決方案。我最後的嘗試是像以下:
class Checkout < Reform::Form
extend ::ActiveModel::Callbacks
#...
property :billing_address, populate_if_empty: Address, form: BillingAddress
property :shipping_address, populate_if_empty: Address, form: ShippingAddress
#...
end
class BillingAddress < Reform::Form
extend ::ActiveModel::Callbacks
include Address
end
class ShippingAddress < Reform::Form
extend ::ActiveModel::Callbacks
include Address
end
module Address
include Reform::Form::Module
property :firstname
property :lastname
property :address1
property :address2
property :phone
property :city
property :zipcode
property :country_id
property :billing_address_for_id
property :shipping_address_for_id
validates :firstname,
:lastname,
:address1,
:phone,
:city,
:zipcode,
:country_id,
presence: true
# provided by phony_rails gem
# validates phone number to be correct and plausible
# without country accordance
validates :phone, phony_plausible: { ignore_record_country_code: true }
# provided by validates_zipcode gem
# validates zipcode to be correct due to country alpha2 code
validates :zipcode, zipcode: { country_code: :country_code }
end
有了這樣的設置
@checkout = Checkout.new(@order)
是給我的錯誤
undefined method `active_record' for #Declarative::Heritage:0x007ff6ea6fb038
那麼,有在改革的任何標準的方法來使事情上述工作?或者有人可以告訴我做錯了什麼?謝謝!
一不留神,我找到了答案。實際的問題是我的模塊名稱是地址什麼是我的一個模型的確切名稱。所以它明顯地造成了一些衝突。 – VAD