2017-02-19 42 views
1

我試圖使用建議的示例from github。您也可以找到它像this one一些答案爲Rails 4Rails 5 - 如何將控制器中整個jsonb postgres列的參數白名單?

我已經對Rails 5.0.1嘗試這樣做,我只是得到一個空的哈希{}存儲在數據庫中:

def proposal_params 
    params.require(:proposal).permit(:document, :account_id).tap do |whitelisted| 
    whitelisted[:document] = params[:proposal][:document] 
    end 
end 

顯然,如果我只是允許!有用。

我也試着回答來自this question

def proposal_params 
    params.require(:proposal).permit(:account_id, document: Proposal.stored_attributes[:document]) 
end 

但也不能正常工作。

The:document屬性包含json,它在請求之間永遠不會相同......並且是一個漫長而複雜的結構。

我只需要將它轉儲到jsonb列。

對於好奇,這裏是一個什麼可以在document一個例子:

{ 
    "document":{ 
     "customer":{ 
     "id":"273a0ad1-0867-4c17-8e0a-3284c6f2f6af", 
     "first_name":"Ricardo", 
     "last_name":"Bird", 
     "email":"[email protected]", 
     "mobile":"07786560223" 
     }, 
     "state":8, 
     "salutation":"Mr & Mrs Bird", 
     "total_price":0, 
     "quoted_products":[ 
     { 
      "product":{ 
       "sku":"9111", 
       "name":"Solid European Oak", 
       "price":25.99, 
       "category":"Wood", 
       "sub_category":"Solid", 
       "updated_at":"2016-12-01", 
       "updated_by":"Donald Duck", 
       "created_at":"2016-11-01", 
       "created_by":"Mickey", 
       "image_url":"http://www.higherground.co.uk/wp-content/uploads/2015/11/wood-flooring-thumbnail.jpg" 
      }, 
      "total_price":25.99, 
      "total_area":1, 
      "product_total_price":25.99, 
      "is_manual_total":false, 
      "is_installed":false, 
      "install_price":null, 
      "are_rooms_grouped":false, 
      "rooms":[ 
       { 
        "name":"Dining Room", 
        "icon_url":"assets/fb-img/dining-room.png", 
        "number":null, 
        "area":1, 
        "width":null, 
        "length":null, 
        "subfloor_prep":null, 
        "subfloor_price":null, 
        "perimeter_product":null, 
        "perimeter_length":null, 
        "is_perimeter_installed":false, 
        "perimeter_price":null, 
        "perimeter_style":null, 
        "is_perimeter_remove_old":false, 
        "is_move_furniture":false, 
        "move_furniture_price":null, 
        "move_surcharge":null, 
        "stairs_stepcount":null, 
        "surcharge":null, 
        "is_installed":false, 
        "uplift_price":null, 
        "install_method":"bonded" 
       } 
      ], 
      "is_extras":true, 
      "threshold_count":2, 
      "radiator_count":3, 
      "trim_count":2, 
      "threshold_price":30, 
      "radiator_price":4, 
      "trim_price":10, 
      "is_rear_mat":false, 
      "is_front_mat":true, 
      "front_mat_type":"Coloured", 
      "rear_mat_type":null, 
      "front_mat_area":2, 
      "front_mat_price":60.01, 
      "rear_mat_area":null, 
      "rear_mat_price":null, 
      "extras_total_price":212.01999999999998 
     } 
     ], 
     "status":"Draft", 
     "is_details_oneprice":false, 
     "notes":"Testing submission" 
    } 
} 
+0

似乎有優秀的提供一個簡單的解決方案,這這裏PR:https://開頭github.com/rails/rails/pull/26308 – rmcsharry

+0

這個問題可能與嘗試允許'document'參數時獲得的警告「Unpermitted parameter:document」有關嗎? – 2017-02-19 23:10:59

+0

@Pyro你的答案解決了它。謝謝!我認爲在這種特殊情況下,未經許可的參數信息可能只是一個紅鯡魚。 – rmcsharry

回答

3

如果我是正確的,你仍然需要permit!文件NT參數:

def proposal_params 
    params.require(:proposal).permit(:account_id).tap do |whitelisted| 
    whitelisted[:document] = params[:proposal].fetch(:document, ActionController::Parameters.new).permit! 
    end 
end 

其工作原理是,它只會保留account_id在第一,但隨後內tap我們通過嘗試從原來的參數檢索它添加document參數後面。 ActionController::Parameters.new作爲fetch的默認值可確保即使沒有傳遞document參數,也始終可調用permit!方法。

在上載參數引擎蓋ActionController::Parameters#permit! seems to recursively call the permit! function爲好,這樣我們就可以把它在任何實例:

def permit! 
    each_pair do |key, value| 
    Array.wrap(value).each do |v| 
     v.permit! if v.respond_to? :permit! 
    end 
    end 

    @permitted = true 
    self 
end 
+0

感謝您的詳細解釋。這工作,謝謝。我在這個問題中添加了一條評論,注意到未來版本的Rails中有一個解決方案可以簡化這個問題的解決方案。 – rmcsharry

相關問題