Spree 3.0 adding to permitted attributes in extension
Asked Answered
P

2

9

So I'm making a spree extension in which I have my own attribute that I added to Spree::Shipment and added an input during the checkout process, the problem is my attribute is not part of the permitted attributes for shipments, and it is not clear how to add it to the permitted attributes. I found the conversation on this pull req which says to use

Spree::PermittedAttributes.shipment_attributes << :my_custom_attribute

However, it is unclear where do I put this!?

"Oh, put it in spree.rb"

This doesn't help. I have tried putting this code in

lib/spree.rb
lib/spree/permitted_attributes.rb
lib/spree_decorator.rb
lib/spree/permitted_attributes_decorator.rb

(as suggested here) and all of these result in either an error complaining about shipment_attributes not being defined (so presumably the code is run before the main file defining PermittedAttributes is evaluated) or simply nothing happens. Where should I put this code to add my attribute to the list of permitted attributes?


Edit: Since this seems unclear to people, I have tried all of the things listed in the links I have posted. Telling me to try the things in them is quite infuriating. Stop doing that.

Prisca answered 17/11, 2015 at 23:40 Comment(5)
"I think it might be better to add it in ApplicationController or some other file that gets reloaded with the app though. You might experience issues where rails would reload the app code and attributes would be missing on that class accessor" - Cited from the github thread... did you tried in ApplicationController?Valladares
Have you tried putting it in an initializer, as was suggested in the update of the article you posted? (rubycoloredglasses.com/2014/04/…)Wiltshire
I also have a couple of custom attributes and I added Spree::PermittedAttributes.shipment_attributes << :my_custom_attribute in config/initializers/spree.rb (at the end) and it worked for meBarto
Did you find any alternative approach? Because, putting it in spree.rb is probably not the best approach.Withe
@anoop No, although I may have fsck'd up my installation so try the solutions I linked, they may work for you even though they didn't work for me.Prisca
R
5

"spree.rb" actually means config/initializers/spree.rb. This is the proper place for spree config. I noticed you/that article mentioned various files, but never this file.

Once in this file either:

Spree::PermittedAttributes.shipment_attributes << :my_custom_attribute

as you mentioned or

Spree::PermittedAttributes.shipment_attributes.push :my_custom_attribute

If that doesn't work you will need to provide more detail.

Rare answered 30/6, 2016 at 19:12 Comment(0)
E
0

As it is stated here you could put it just into ApplicationController.

Or you could override the whole permitted_params,adding yours to whitelisted (by either putting this code straight into application_controller.rb after last end, which will 100% work, or by creating a new file(for example under lib, as you have already tried)):

module Spree
  module PermittedAttributes

    # bunch of code

    @@checkout_attributes = %i(
      email
      use_billing
      shipping_method_id
      coupon_code
      my_custom_attribute
    )

    # bunch of code

  end
end
Epidemic answered 26/11, 2015 at 20:34 Comment(2)
See my edit about telling me to try things in the links I posted.Prisca
I did not mean to irritate or provoke you. I do not imagine a case though, in which my solution with app_controller would not affect the situation. But, as you say soEpidemic

© 2022 - 2024 — McMap. All rights reserved.