[英]rails 4 update only nested attributes strong parameters
單擊某個提交按鈕時,我僅嘗試更新嵌套屬性,但是我無法弄清楚如何僅將那些參數傳遞給更新操作。
def update
if params[:commit] == "Update Dogs"
owner.update_attributes(params[dogs_attributes: [:id, :name, :tag]])
render :show
...
end
end
def owner_attributes
params.fetch(:owner).permit(:id, :name, :address, dogs_attributes: [:id, :name, :tag])
end
它不會提交更改。 我也嘗試過
owner.update_attributes(dogs_attributes)
但隨后出現錯誤“未定義的方法dogs_attributes”。 試圖將其定義為類似於owner_attributes的自己的方法,但是ActionController無法獲取param:dog。 我已經嘗試了以上的所有變種,但都沒有用。 這可能是一個簡單的錯誤,但我不知道如何正確編寫此錯誤。 有任何想法嗎?
PS。 所有者確實接受狗的嵌套屬性。
謝謝!
為什么不只為update方法指定不同的set屬性?
def update
if params[:commit] == "Update Dogs"
Owner.update_attributes(update_attr)
else
Owner.update_attributes(owner_attr)
end
end
private
def update_attr
params.require(:owner).permit(:dog_attributes[:id, :name, :tag])
end
def owner_attributes
params.fetch(:owner).permit(:id, :name, :address, dogs_attributes: [:id, :name, :tag])
end
加:
accepts_nested_attributes_for :dogs
到您的Owner
模型
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.