2017-06-02 116 views
0

我正在構建一個API端點來更新模型。除了嵌套的資源,我可以更新每個列,我已經嘗試了不同的技術途徑,但似乎沒有任何工作通過API/Json更新嵌套資源

這是我試圖發送到服務器的JSON

{ 
"reservation": { 
    "reservation_dates": [ 
     { 
      "is_desirable": true, 
      "date": "5-10-2019" 
     } 
     ] 
    } 
} 

我得到一個unpermitted_pa​​ram從reservation_date雖然我已經把它添加到我的

def permitted_attributes_for_update 
params.require(:reservation).permit(:date, :time, :comment, :budget, :currency, :status, 
            :general_text, :idea_text, :artist_text, :desired_city, 
            :desired_country, :desired_googleid, :studio_id, :artist_id, 
            :tattoos, reservation_dates: [], general_url_array: [],idea_url_array: [], 
            artist_url_array: []) 
end 

我想要麼能夠直接從JSON更新,或者至少允許陣列,所以我可以在我的UpdateService

以後使用

感謝您的幫助

edit: this is the error I'm getting

+0

用確切的錯誤更新問題。 – Pavan

+0

你允許在模型中嵌套屬性嗎? – 8bithero

+0

我有@ 8bithero –

回答

0

你需要指定reservation_dates什麼被允許:

params.require(:reservation).permit(reservation_dates_attributes: [:is_desirable, : date], ...)

但是,要小心,如果你的reservation的has_many reservation_dates,那麼這將導致衝突:reservation_dates應該是ReservationDate的實例,但是您提供了散列。軌道的方式是使用reservation_dates_attributes而不是reservation_dates

+0

我試過了。沒有它,它更新了其他非嵌套字段。如果我將這些符號添加到我的許可證中,則會破壞整個事件 –

+0

查看更新,「它破壞了整個事物」意味着參數設法獲得許可,但其存在會引起衝突。 – wesley6j

+0

現在我得到「未經許可的paramiter::reservation_dates_attributes」 –