2017-06-02 119 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” –