Given Reed
06/13/2023, 12:33 PMspree.rb
, set up the association with the new address to the order, all the new fields show up just like I want them to.
The fun comes from the client wanting to move one of the new fields to a different section in the checkout. It worked perfectly when it was in the address step, but they want it all the way out on the confirm step. I've got some code in the update method of checkouts_controller.rb
that logs out the value I want, has everything I need, but when I try to save the value (still associated with the address for now) I get:
ActiveRecord::ReadOnlyRecord in CheckoutsController#update
Spree::Address is marked as readonly
The code I'm trying to use is:
@order.sub_address.lockbox_code = params['order']['checkout_confirm_attributes']['lockbox_code']
@order.save!
I've also tried instantiating a Spree::Address model specifically to update it directly instead of through the relationships, in case that made a difference, but it gave the same result.
I saw this but that looks like how the controller is doing it now:
Spree::OrderUpdateAttributes.new(@order, update_params, request_env: request.headers.env).apply
but while I work on that I was wondering if I was just taking the wrong approach entirely 😛kennyadsl
def sub_address_attributes=(attributes)
self.sub_address = Spree::Address.immutable_merge(sub_address, attributes)
end
At that point, your code should allow to save it!Given Reed
06/13/2023, 1:32 PMnew_params[:sub_address_attributes][:lockbox_code] = params['order']['checkout_confirm_attributes']['lockbox_code']
Spree::OrderUpdateAttributes.new(@order, new_params, request_env: request.headers.env).apply
After testing this, I saw the value that I entered in the confirm step get saved and appear in the admin.