gpt4 book ai didi

ruby-on-rails - 改革 gem : use one model for multiple forms

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:07 31 4
gpt4 key购买 nike

我正在使用 Reform gem 制作一个用于在我的网上商店结帐的表单对象。我有 Checkout 表单,其中包含 Order 模型的属性,该模型与 Address 模型有关联。

问题是 Order 模型与同一个 Address 模型有两个关联:

class Order < ActiveRecord::Base
#...
belongs_to :billing_address, :class_name => 'Address'
belongs_to :shipping_address, :class_name => 'Address'
#...
end

所以我需要设置我的 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

那么 Reform 中是否有任何标准方法可以使上面的事情起作用?或者任何人都可以告诉我做错了什么?谢谢!

最佳答案

偶然我找到了答案。实际问题是我的模块名称是 Address,这正是我的一个模型的名称。这显然引起了一些冲突。所以我将 Address 更改为 AddressModule。

我的工作设置将是:

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 AddressModule
end

class ShippingAddress < Reform::Form
extend ::ActiveModel::Callbacks
include AddressModule
end

module AddressModule
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

关于ruby-on-rails - 改革 gem : use one model for multiple forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36861627/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com