Ruby on rails 当我';m使用接受\u嵌套的\u属性\u

Ruby on rails 当我';m使用接受\u嵌套的\u属性\u,ruby-on-rails,activerecord,ruby-on-rails-5,rails-api,Ruby On Rails,Activerecord,Ruby On Rails 5,Rails Api,使用postman,我试图发布一个新的配方,它具有标签和配料的嵌套属性。下面的模型显示了这些关系 #配方模型 类配方:recipe\u配料 有多个:标记,:至=>:配方\u标记 接受:配料、:标记的\u嵌套\u属性\u 结束 #标签模型 类标记:菜谱\u标签 def self.add_段塞 更新(slug:to_slug(tag_name)) 结束 def至_参数 鼻涕虫 结束 结束 #成分模型 类组件:配方 结束 #加入食谱和配料表 类RecipeIngElement

使用postman,我试图发布一个新的配方,它具有标签和配料的嵌套属性。下面的模型显示了这些关系

#配方模型
类配方<应用程序记录
有很多:配方和配料
有很多:配方标签
有很多:配料,:通过=>:recipe\u配料
有多个:标记,:至=>:配方\u标记
接受:配料、:标记的\u嵌套\u属性\u
结束
#标签模型
类标记<应用程序记录
有很多:配方标签
有很多:菜谱,:至=>:菜谱\u标签
def self.add_段塞
更新(slug:to_slug(tag_name))
结束
def至_参数
鼻涕虫
结束
结束
#成分模型
类组件<应用程序记录
有很多:配方和配料
有很多:配方,:至=>:配方
结束
#加入食谱和配料表
类RecipeIngElement<应用程序记录
属于:配方,可选:真
属于:成分,可选:真
结束
#配方和标签的联接表
类RecipeTag
这是我处理请求的配方控制器

class Api::RecipesController < ApplicationController
  #before_action :authenticate_user

  def index
    @recipes = Recipe.all
    render json: @recipes, status: 200
  end

  def create
    @recipe = Recipe.new(recipe_params)
    #ingredient = @recipe.ingredients.build
    render json: @recipe, status: 200
  end

  def show
    @recipe = Recipe.find(params[:id])
    render json: @recipe, status: 200
  end

  private

  def recipe_params
    params.require(:recipe).permit(:name, :description, ingredients_attributes: [:id, :description], tags_attributes: [:id, :tag_name])
  end
end
我得到的是一个新创建的配方,但是配料和标签数组都是空的。在我的控制台里

Unpermitted parameters: :ingredients, :tags

我想知道为什么在我使用accepts\u nested\u attributes\u时没有保存这些参数。谢谢

更新

问题在于《邮递员》中的尸体以及将成分和标签更改为成分属性和标签属性。下面是我的RecipeController实际创建配方的更新rails创建方法。谢谢

  def create
    @recipe = Recipe.new(recipe_params)
    @ingredients = recipe_params["ingredients_attributes"]
    @tags = recipe_params["tags_attributes"]
    @ingredients.each do |ingredient|
      @recipe.ingredients.build(ingredient)
    end

    @tags.each do |tag|
      @recipe.tags.build(tag)
    end

    if @recipe.save
      render json: @recipe, status: 200
    else
      render json: @recipe.errors, status: :unprocessable_entry
    end
  end


这是因为您发送的属性是
Postman
不允许的。尝试修改JSON属性,特别是用
成分属性
替换
成分
,用
标签
替换
标签

您的最终JSON正文应该如下所示:

{
  "recipe":
  {
    "name": "Creamy Dill Chicken", 
    "description": "Dill has fresh and grassy flavor. Give it a small taste first if you are unfamiliar with the herb, and feel free to leave out some or all of it if too strong.", 
    "ingredients_attributes": [
      ...
    ], 
    "tags_attributes": [
      ...
    ]
  }
}

这是因为您发送的属性是
Postman
不允许的。尝试修改JSON属性,特别是用
成分属性
替换
成分
,用
标签
替换
标签

您的最终JSON正文应该如下所示:

{
  "recipe":
  {
    "name": "Creamy Dill Chicken", 
    "description": "Dill has fresh and grassy flavor. Give it a small taste first if you are unfamiliar with the herb, and feel free to leave out some or all of it if too strong.", 
    "ingredients_attributes": [
      ...
    ], 
    "tags_attributes": [
      ...
    ]
  }
}

成功了!谢谢如果我要在react中实现一个表单,我的帖子正文是否应该使用相同的名称(成分属性和标签属性)?@AlexMcKean是的,你必须使用相同的名称。就是这样!谢谢如果我要在react中实现一个表单,我的帖子正文是否应该使用相同的名称(成分属性和标签属性)?@AlexMcKean是的,您必须使用相同的名称。