更新rails中的嵌套属性 [英] Update nested attributes in rails
问题描述
我在传送修补请求时,无法更新巢状属性。我想更新我的recipe_ingredients当我更新我的食谱。每次当我更新我的食谱,食谱获得更新,但recipe_ingredients只是附加的那个食谱。请帮助〜非常感谢〜
配方控制器
```
def update
@recipe = Recipe.find(params [:id])
if @ recipe.update(recipe_params)
@recipe_ingredients = @ recipe.recipe_ingredients.all
head:no_content
else
render json:@ recipe.errors,status::unprocessable_entity
end
end
def recipe_params
params.require(:recipes)
.permit(:name,:category,instructions:[],recipe_ingredients_attributes:[:id,:recipe_id,:ingredient,:measure ,:amount])
end
```
配方模型:
```
has_many:recipe_ingredients,dependent::destroy
accepts_nested_attributes_for:recipe_ingredients,allow_destroy:true,update_only:true
end
```
Curl请求:
```curl --include --request PATCH http:// localhost:3000 / recipes / 1 \
--header授权:令牌令牌= ...\
--headerContent-Type:application / json\
--data'{
recipes:{
name :second it recipe,
category:grill,
instructions:[do it,ignore it],
recipe_ingredients_attributes:[{
amount:1,
ingredient:egg yolk,
measure:cup
},
{
amount :3,
ingredient:豆浆,
measure:杯
}]
}
}'
```
解决方案p>您需要在curl请求中的
recipe_ingredients
中发送id
以更新正确的现有recipe_ingred
记录。否则,rails将创建一个新记录。例如,这将更新recipe_ingred
与id 1
并创建新的豆浆成分:
recipe_ingredients_attributes:[{
id:1
amount:1,
蛋黄,
measure:cup
},
{
amount:3,
ingredient:豆浆 b $ bmeasure:cup
}]I am having trouble with updating my nested attributes when I sending patch request. I want to update my recipe_ingredients when I update my recipe. Every time when I update my recipe, recipe gets updated, but recipe_ingredients are just appending for that recipe. Please help~ Many thanks~
Recipe Controller
```
def update @recipe = Recipe.find(params[:id]) if @recipe.update(recipe_params) @recipe_ingredients = @recipe.recipe_ingredients.all head :no_content else render json: @recipe.errors, status: :unprocessable_entity end end def recipe_params params.require(:recipes) .permit(:name, :category, instructions: [], recipe_ingredients_attributes: [:id, :recipe_id, :ingredient, :measure, :amount]) end
```
Recipe Model:
```
class Recipe < ActiveRecord::Base has_many :recipe_ingredients, dependent: :destroy accepts_nested_attributes_for :recipe_ingredients, allow_destroy: true, update_only: true end
```
Curl Request: ```
curl --include --request PATCH http://localhost:3000/recipes/1 \ --header "Authorization: Token token=..." \ --header "Content-Type: application/json" \ --data '{ "recipes": { "name": "second example recipe", "category": "grill", "instructions": ["do it", "ignore it"], "recipe_ingredients_attributes": [{ "amount": 1, "ingredient": "egg yolk", "measure": "cup" }, { "amount": 3, "ingredient": "soy milk", "measure": "cup" }] } }'
```
解决方案You need to send
id
of yourrecipe_ingredients
in the curl request for rails to update the correct existingrecipe_ingredient
records. Otherwise, rails will create a new record. For example, this will updaterecipe_ingredient
withid 1
and create new "soy milk" ingredient:
"recipe_ingredients_attributes": [{ "id": 1 "amount": 1, "ingredient": "egg yolk", "measure": "cup" }, { "amount": 3, "ingredient": "soy milk", "measure": "cup" }]
这篇关于更新rails中的嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!