2015-11-17 50 views
7

所以我正在做一個大禮包擴展中,我有我自己的屬性,我加入到Spree::Shipment並添加在結帳過程的輸入,問題是我的屬性是不部分允許的貨件屬性,並且不清楚如何將其添加到允許的屬性中。我發現在談話上this pull req它說,使用施普雷3.0添加到允許的屬性在擴展

Spree::PermittedAttributes.shipment_attributes << :my_custom_attribute 

但是,目前還不清楚在哪裏我把這個!?

「哦,把它放在spree.rb

這並沒有幫助。我試圖把這個代碼在

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

as suggested here)和所有這些結果的任一錯誤抱怨shipment_attributes沒有被定義(因此推測代碼主文件中定義PermittedAttributes求值之前運行),或僅僅什麼都沒發生。我應該在哪裏添加我的屬性到允許的屬性列表中?


編輯:由於這似乎不清楚的人,我已經嘗試了我已發佈的鏈接中列出的所有事情。告訴我去嘗試他們中的東西是非常令人憤怒的。別那樣做。

+0

「我認爲這可能是更好的將其添加到ApplicationController中或獲取與應用,雖然重新加載一些其他的文件,您可能會遇到在那裏的軌道將重新加載應用程序代碼和屬性會在該類訪問丟失的問題。」 - 引從github線程...你在ApplicationController中試過了嗎? – SsouLlesS

+0

你試過把它放在一個初始化,如您發佈的文章的更新建議? (http://www.rubycoloredglasses.com/2014/04/strong-parameters-with-spree-extensions/) – jphager2

+0

我也有一對夫婦自定義屬性的,我接着說:狂歡:: PermittedAttributes.shipment_attributes <<:my_custom_attribute'在配置/初始化/ spree.rb(末)和它的工作對我來說 –

回答

0

正如所述here你可以把它放進ApplicationController

或者你也可以覆蓋整個permitted_pa​​rams,增加你列入白名單(通過把這個代碼直入application_controller.rb最後end後,其將100%的工作,或通過創建lib下一個新的文件(例如,你有已經嘗試過)):

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 
+0

看到我的編輯關於告訴我嘗試在我發佈的鏈接中的東西。 – Shelvacu

+0

我不是故意刺激或激怒你。我不想像一個案例,其中我的解決方案與app_controller不會影響情況。但是,正如你所說的那樣 –

2

「spree.rb」實際上是指config/initializers/spree.rb。這是spree config的適當位置。我注意到你/文章提到了各種文件,但從來沒有這個文件。

一旦在這個文件之一:

Spree::PermittedAttributes.shipment_attributes << :my_custom_attribute 

正如你所提到或

Spree::PermittedAttributes.shipment_attributes.push :my_custom_attribute 

如果不工作,你將需要提供更多的細節。