Getting all notes to display on the front-end

I built out a recipe book for my wife and her family and I seem to be having issues with getting the notes that people add to display, mainly when someone posts a note it replaces the previous note with the new one instead of displaying all of the notes. I tried adding a notes table and controller and all of that and it kept throwing errors and just wouldn’t work. So I have tried to make a model method to just show all of the notes but it gives me an error in my update action. This is my first real project out of my Bootcamp so I am still fairly green at all of this but I want to show my controller and model with the method that I thought that would work. I don’t know if I was going down the right track with the model and controller and just didn’t quite get all of the syntax right or if the model method is the correct path.

Model:

class Recipe < ApplicationRecord
  validates :name, presence: true

  def ingredients_list
    ingredients.split(", ")
  end

  def directions_list
    directions.split(". ")
  end

  def recipe_notes
    recipe_notes = notes.all
  end 
end

Controller:

class RecipesController < ApplicationController

  def index
    recipes = Recipe.all
    render json: recipes
  end 

  def show
    recipe = Recipe.find_by(id: params[:id])
    render json: recipe
  end 

  def create
    recipe = Recipe.new(
      name: params["name"],
      contributor: params["contributor"],
      ingredients: params["ingredients"],
      directions: params["directions"],
      category: params["category"]
    )

    if recipe.save
      render json: recipe
    else
      render json: {errors: recipe.errors.full_messages},
      status: 422
    end
  end 

  def update
    recipe_id = params["id"]
    recipe = Recipe.find_by(id: recipe_id)

    recipe.name = params["name"] || recipe.name
    recipe.contributor = params["contributor"] || recipe.contributor
    recipe.ingredients = params["ingredients"] || recipe.ingredients
    recipe.directions = params["directions"] || recipe.directions
    recipe.category = params["category"] || recipe.category
    recipe.notes = params["notes"] || recipe.notes

    if recipe.save
      render json: recipe
    else 
      render json: {errors: recipe.errors.full_messages},
      status: 422
    end 
  end
  
  def destroy
    recipe_id = params["id"]
    recipe = Recipe.find_by(id: recipe_id)

    recipe.destroy
    render json: {message: "Recipe Deleted"}
  end 
end

This was my schema before I removed all of the notes stuff:

create_table "notes", force: :cascade do |t|
    t.integer "recipe_id"
    t.string "note"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "recipes", force: :cascade do |t|
    t.string "name"
    t.string "ingredients"
    t.string "directions"
    t.string "category"
    t.string "contributor"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "notes"
  end

This was the model before I tried to move to a model method:

class Recipe < ApplicationRecord
  validates :name, presence: true
  has_many :notes 

  def ingredients_list
    ingredients.split(", ")
  end
  def directions_list
    directions.split(". ")
  end
end

class Note < ApplicationRecord
  belongs_to :recipe
end

This was the controller that I set up for the notes before I removed it:

class NotesController < ApplicationController

  def show
    note = Note.find_by(id: params[:id])
    render json: note
  end 
end

I also had the route set up for a show action too

Very cool idea - your wife is going to love it :woman_cook:t3:

I recategorized this so it will have better visibility among those who are able to assist :smile:

1 Like