Alex McKean
Alex McKean

Reputation: 47

Why am I not able to save my nested attributes when I'm using accepts_nested_attributes_for

Using postman, I am trying to POST a new recipe that has the nested attributes of tags and ingredients. The Relationships are shown in the models below.

#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


This is my recipe controller handling the request.

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

The params I'm sending in from postman are

{
    "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"
            }
            ]
    }
}

What I get back is a newly created recipe, but the ingredients, and tag arrays are empty. In my console I get

Unpermitted parameters: :ingredients, :tags

I'm wondering why these parameters aren't being saved when I'm using accepts_nested_attributes_for. Thanks.

UPDATE

Issue was with the body in PostMan and changing ingredients and tags to ingredients_attributes and tags_attributes. Below is an updated rails create method for my RecipeController to actually create the recipe. Thanks!

  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

Upvotes: 0

Views: 216

Answers (1)

Daniel Batalla
Daniel Batalla

Reputation: 1264

That is because you are sending attributes that are not permitted with Postman. Try modifying JSON attributes, specifically replace ingredients with ingredients_attributes and tags with tags_attributes.

Your final JSON body should look like this:

{
  "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": [
      ...
    ]
  }
}

Upvotes: 2

Related Questions