0

I am using, for better or worse, the Spree commerce gem. I have added a new attribute and column to the Spree::Order model and am overriding the update_registration action on the CheckoutController. When calling the update_registration action, I can see the contents of the form being sent via params:

Processing by Spree::CheckoutController#update_registration as HTML "order"=>{"email"=>"[email protected]", "new_attribute"=>"Bob"}, "commit"=>"Continue to checkout"

Which is process by a simple command object:

def call
  order.update(params)
end

There are no log entries following that indicating any form of problem or rollback. I then see:

Spree::Order Update (1.4ms) UPDATE "spree_orders" SET "email" = $1, "updated_at" = $2 WHERE "spree_orders"."id" = $3 [["email", "[email protected]"], ["updated_at", "2020-07-23 11:43:29.461912"], ["id", 338]]

Which succeeds. Seemingly, the update method on the model is ignoring the new attribute. I can confirm this in via the console in the database.

I have specs that demonstrate the desired behaviour and indeed it works locally. I am tearing my hair out, quite literally, and I don't have that much to start with. This isn't my first Rails rodeo but cannot for the life of me understand what is going on here. It is bizarre.

1
  • 1
    Never used Spree, but after looking its code for 10sec I'd bet 10vs1 that you have to deal with "strong parameters". In other words you should look for "how to permit new attribute in XXX controller" in the documentation. github.com/spree/spree/blob/… Commented Jul 24, 2020 at 6:01

2 Answers 2

0

You need to add your new attribute to permitted attributes in the spree initializer

# config/initializers/spree.rb
Spree::PermittedAttributes.checkout_attributes << :other_attribute

Check https://github.com/spree/spree/blob/master/core/lib/spree/core/controller_helpers/strong_parameters.rb#L19

0

This has been solved, it was nothing to do with strong params. Seemingly, there is a difference in class loading between production and development environments which isn't made clear in Spree's docs nor is it even referenced.

In production my controller action override was being clobbered by the default Spree implementation. It just so happened that the original code was doing something similar so it looked like it was ignoring a param. Removing my code altogether and observing an update being executed demonstrated that my code paths were not being run.

The override pattern in Spree is somewhat clunky, in my experience it is cleaner to implement your own paths and use Spree to drop back into the gem's flow rather than trying to override.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.