ga-wdi-boston / full-stack-project

Other
8 stars 64 forks source link

When I patch I get a 200 but the data doesnt update... console says unpermitted parameter #286

Closed louarnos closed 8 years ago

louarnos commented 8 years ago

I have a joined table, meal_items, that links meals and foods. I want to patch the meals associated with a users given meal. Meals sees the foods for a given meal as food_for_meals. This is my curl request:

TOKEN='BAhJIiUzNDliMWRhOTU3NWUxYTBkZTFhYjNhNWVmNTAyNDM5MAY6BkVG--40b21089137c302f4e7a07a52a6c7caa804e1bc3'
ID='15'

curl --include --request PATCH http://localhost:3000/meals/$ID \
  --header "Authorization: Token token=$TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "meal" : {
    "food_for_meals" : [1,2,3,4,5,6,7,8,9]
  }
}'

This is my controller for meals:

class MealsController < ProtectedController
  before_action :set_meal, only: [:show, :update, :destroy]
  # accepts_nested_attributes_for :meal_items, :food_for_meals

  # GET /meals
  # GET /meals.json
  def index
    @meals = current_user.meals
    render json: @meals
  end

  # GET /meals/1
  # GET /meals/1.json
  def show
    render json: Meal.find(params[:id])
  end

  # POST /meals
  # POST /meals.json
  def create
    # binding.pry
    @meal = current_user.meals.build(meal_params)

    if @meal.save
      render json: @meal, status: :created, location: @meal
    else
      render json: @meal.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /meals/1
  # PATCH/PUT /meals/1.json
  def update
    @meal = Meal.find(params[:id])

    if @meal.update(meal_params)
      render json: @meal, status: :ok
    else
      render json: @meal.errors, status: :unprocessable_entity
    end
  end

  # DELETE /meals/1
  # DELETE /meals/1.json
  def destroy
    @meal.destroy

    head :no_content
  end

  def set_meal
    @meal = current_user.meals.find(params[:id])
  end

  def meal_params
    params.require(:meal).permit(:meal_type, :meal_items, :food_for_meals)
  end

  private :set_meal, :meal_params
end

This is my meal model:

class Meal < ActiveRecord::Base
  belongs_to :user, inverse_of: :meals
  has_many :meal_items, inverse_of: :user_meal, foreign_key: 'user_meal_id'
  has_many :food_for_meals, through: :meal_items
end

I have tried patching with both meal_items and food_for_meals as the key and I get the same response from the console.

louarnos commented 8 years ago

I talked to Natalie who also has a joined table and she says I need to post to meal Items to accomplish what I'd like to do.

RealWeeks commented 8 years ago

Try it. What happens?

RDegnen commented 8 years ago

Wait are you trying to update a meal, or the foods that are associated with that meal?

louarnos commented 8 years ago

the foods that belong to a meal

louarnos commented 8 years ago

This is the post I'm working with now:

TOKEN='BAhJIiU2ZjA5YWUzYmNkMzc4ZjFhZjc5Y2ZjNzU4M2ZjZTcxZQY6BkVG--bd4a857908df5f3bc9015aa3e7b2cf89cb710c79'

curl --include --request POST http://localhost:3000/meal_items \
  --header "Authorization: Token token=$TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "meal_item" : {
    "user_meal_id" : 15,
    "food_for_meal_id" : 4
  }
}'

This is my meal_items controller

class MealItemsController < ApplicationController
  before_action :set_meal_item, only: [:show, :update, :destroy]

  # GET /meal_items
  # GET /meal_items.json
  def index
    @meal_items = MealItem.all

    render json: @meal_items
  end

  # GET /meal_items/1
  # GET /meal_items/1.json
  def show
    render json: @meal_item
  end

  # POST /meal_items
  # POST /meal_items.json
  def create
    @meal_item = MealItem.new(meal_item_params)

    if @meal_item.save
      render json: @meal_item, status: :created, location: @meal_item
    else
      render json: @meal_item.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /meal_items/1
  # PATCH/PUT /meal_items/1.json
  def update
    @meal_item = MealItem.find(params[:meal_id])

    if @meal_item.update(meal_item_params)
      render json: @meal_item, status: :ok
    else
      render json: @meal_item.errors, status: :unprocessable_entity
    end
  end

  # DELETE /meal_items/1
  # DELETE /meal_items/1.json
  def destroy
    @meal_item.destroy

    head :no_content
  end

  private

    def set_meal_item
      @meal_item = MealItem.find(params[:id])
    end

    def meal_item_params
      params.require(:meal_item).permit(:food_id, :meal_id, :food_for_meal_id, :user_meal_id, :serv_qty)
    end
end

server says:

Started POST "/meal_items" for ::1 at 2016-05-03 09:58:15 -0400
Processing by MealItemsController#create as */*
  Parameters: {"meal_item"=>{"user_meal_id"=>15, "food_for_meal_id"=>4}}
   (0.1ms)  BEGIN
  SQL (1.0ms)  INSERT INTO "meal_items" ("food_for_meal_id", "user_meal_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["food_for_meal_id", 4], ["user_meal_id", 15], ["created_at", "2016-05-03 13:58:15.019165"], ["updated_at", "2016-05-03 13:58:15.019165"]]
   (0.5ms)  COMMIT
[active_model_serializers] Rendered MealItemSerializer with ActiveModel::Serializer::Adapter::Json (4.78ms)
Completed 500 Internal Server Error in 15ms (ActiveRecord: 1.6ms)

NoMethodError (undefined method `food' for #<MealItem:0x007fb0cbf8ab50>):
  app/controllers/meal_items_controller.rb:24:in `create'
louarnos commented 8 years ago

I'm confused about the best approach at this point.

RDegnen commented 8 years ago

can you just updated the foods directly that are associated with that meal instead?

louarnos commented 8 years ago

i tried that above. Thats what I was originally doing.

RDegnen commented 8 years ago

It looks like you were trying to update meals. Not foods

louarnos commented 8 years ago

Oh I see what you're asking. Foods can belong to many meals through meal items tho.

RDegnen commented 8 years ago

yea, but each meal will have foods associated with them. And those foods have an id, so if you update that food then it should be updated when you look for it through a meal

louarnos commented 8 years ago

Foods have no foreign key

RDegnen commented 8 years ago

They are still associated through the join table tho

louarnos commented 8 years ago

So you want me to patch what exactly? I'm confused.

louarnos commented 8 years ago

I've tried patching meal and meal_items... I'm not sure how to patch foods to accomplish this.

RDegnen commented 8 years ago

ill assign myself

louarnos commented 8 years ago

needed to change meal_item serializer to match the names in the table. Posting to meal_items is now working.

louarnos commented 8 years ago

@RDegnen help.