【问题标题】:Why am I not able to save my nested attributes when I'm using accepts_nested_attributes_for为什么我在使用 accept_nested_attributes_for 时无法保存嵌套属性
【发布时间】:2019-06-05 20:40:05
【问题描述】:

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

#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 时这些参数没有被保存。谢谢。

更新

问题在于 PostMan 中的正文并将成分和标签更改为成分属性和标签属性。下面是一个更新的 rails create 方法,供我的 RecipeController 实际创建配方。谢谢!

  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

【问题讨论】:

    标签: ruby-on-rails activerecord ruby-on-rails-5 rails-api


    【解决方案1】:

    那是因为您正在发送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": [
          ...
        ]
      }
    }
    

    【讨论】:

    • 做到了!谢谢!如果我要在 react 中实现一个表单,我应该为我的 POST 正文使用相同的名称(ingredients_attributes 和 tags_attributes)吗?
    • @AlexMcKean 是的,您必须使用相同的名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多