Я добавил пользовательское поле в свою таблицу spree_orders (назовем его custom_attribute
).
Я добавил Spree::PermittedAttributes.checkout_attributes << [:custom_attribute]
в мой инициализатор spree.rb.
В моем процессе оформления заказа у меня есть пользовательская форма со следующим кодом (форматирование html было удалено):
<%= form_for @order do |alt_form| %>
<%= alt_form.label :custom_attribute, "Custom Attribute" %><span class="required">*</span><br />
<%= alt_form.text_field :custom_attribute, :class => 'form-control required', maxlength: 11 %>
<% end %>
Эта форма успешно отправляет поле почтового запроса (полный дамп ниже) на http://localhost:3000/checkout/update/address как order[custom_attribute] xyz
, однако информация не сохраняется в модели.
_method=patch
_method=patch
authenticity_token=Y+ATRotWKfI57f+b0/YGwIw9Bg6mADHBDmeEOHYzLPnB6Vbydya4ITDTopcX65EG+TiL7bwyJKQPpBU9bQTaUg==
authenticity_token=Y+ATRotWKfI57f+b0/YGwIw9Bg6mADHBDmeEOHYzLPnB6Vbydya4ITDTopcX65EG+TiL7bwyJKQPpBU9bQTaUg==
commit=Save and Continue
order[bill_address_attributes][address1]=123 Test
order[bill_address_attributes][address2]=
order[bill_address_attributes][city]=Test
order[bill_address_attributes][country_id]=232
order[bill_address_attributes][firstname]=Test
order[bill_address_attributes][id]=3
order[bill_address_attributes][lastname]=Test
order[bill_address_attributes][phone]=555555555
order[bill_address_attributes][state_id]=3535
order[bill_address_attributes][zipcode]=30024
order[email][email protected]
order[custom_attribute]=2414
order[state_lock_version]=32
utf8=✓
utf8=✓
Я вставил @order.inspect на следующую (платежную) страницу, чтобы увидеть, что @order.custom_attribute по-прежнему равен нулю.
Кто-нибудь знает, что мне нужно сделать, чтобы получить значение custom_attribute
, отправленное в почтовом запросе, сохраненном в модели с другими отправленными атрибутами?
-------------------редактировать-------------------
Разрешенные атрибуты spree по умолчанию определяются здесь https://github.com/spree/spree/blob/3-0-stable/core/lib/spree/core/controller_helpers/strong_parameters.rb и добавляются здесь помощником strong_paramaters
(don у меня нет представителя, чтобы опубликовать третью ссылку):
module Spree
module Core
module ControllerHelpers
module StrongParameters
def permitted_attributes
Spree::PermittedAttributes
end
delegate *Spree::PermittedAttributes::ATTRIBUTES,
to: :permitted_attributes,
prefix: :permitted
def permitted_payment_attributes
permitted_attributes.payment_attributes + [
source_attributes: permitted_source_attributes
]
end
def permitted_checkout_attributes
permitted_attributes.checkout_attributes + [
bill_address_attributes: permitted_address_attributes,
ship_address_attributes: permitted_address_attributes,
payments_attributes: permitted_payment_attributes,
shipments_attributes: permitted_shipment_attributes
]
end
def permitted_order_attributes
permitted_checkout_attributes + [
line_items_attributes: permitted_line_item_attributes
]
end
def permitted_product_attributes
permitted_attributes.product_attributes + [
product_properties_attributes: permitted_product_properties_attributes
]
end
end
end
end
end
который можно найти spree/core/lib/spree/core/controller_helpers/strong_parameters.rb
в репозитории github.
-------------------финальное редактирование----- Если кто-то найдет это в будущем и попытается чтобы устранить аналогичную проблему, мой код выше действительно правильный; Я (по глупости) поместил его в блок if Rails.env.production?
.