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

时间:2019-06-05 20:40:05

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

使用邮递员,我正在尝试发布具有标签和配料嵌套属性的新配方。关系显示在以下模型中。

#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。以下是我的RecipeController实际创建配方的更新的rails create方法。谢谢!

  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

1 个答案:

答案 0 :(得分:2)

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