为什么我在使用accepts_nested_attributes_for时无法保存嵌套属性



使用邮递员,我正在尝试发布一个具有标签和成分嵌套属性的新食谱。关系显示在下面的模型中。

#Recipe Model
class Recipe < ApplicationRecord
  has_many :recipe_ingredients
  has_many :recipe_tags
  has_many :ingredients, :through => :recipe_ingredients
  has_many :tags, :through => :recipe_tags
  accepts_nested_attributes_for :ingredients, :tags
end
#Tag Model 
class Tag < ApplicationRecord
  has_many :recipe_tags
  has_many :recipes, :through => :recipe_tags
  def self.add_slugs
   update(slug: to_slug(tag_name))
  end
  def to_param
   slug
 end
end

#Ingredient Model
class Ingredient < ApplicationRecord
  has_many :recipe_ingredients
  has_many :recipes, :through => :recipe_ingredients
end

#Join Table for recipe and ingredients
 class RecipeIngredient < ApplicationRecord
  belongs_to :recipe, optional: true
  belongs_to :ingredient, optional: true
 end
#Join Table For recipe and tags 
class RecipeTag < ApplicationRecord
  belongs_to :recipe, optional: true
  belongs_to :tag, optional: true
end

这是我处理请求的配方控制器。

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

我从邮递员寄来的参数是

{
    "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":[
            {
                "description": "Dill"
            }, 
            {
                "description": "Yukon Gold Potatoes"
            },
            {
                "description": "Asparagues"
            },
            {
                "description": "Chicken Breasts"
            },
            {
                "description": "Sour Cream"
            },
            {
                "description": "Chicken Stock concentrate"
            },
            {
                "description": "Dijon mustard"
            }
            ], 
        "tags": [
            {
                "tag_name": "Home Cooking"
            },
            {
                "tag_name": "American"
            }
            ]
    }
}

我得到的是一个新创建的食谱,但成分和标签数组是空的。在我的控制台中,我得到

Unpermitted parameters: :ingredients, :tags

我想知道为什么当我使用 accepts_nested_attributes_for 时没有保存这些参数。谢谢。

更新

问题在于邮递员中的身体以及将成分和标签更改为ingredients_attributes和tags_attributes。下面是我的配方控制器实际创建配方的更新的轨道创建方法。谢谢!

  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 属性,特别是将 ingredients 替换为 ingredients_attributes,将tags替换为 tags_attributes

最终的 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": [
      ...
    ]
  }
}

相关内容

  • 没有找到相关文章

最新更新