ga-wdi-boston / full-stack-project

Other
8 stars 64 forks source link

Trying to add a new reference to a table, strong params conflict #963

Closed slammyde7113 closed 7 years ago

slammyde7113 commented 7 years ago

I added a new table table to my database to hold coupon values but I'm having trouble connecting this new table to my Profiles table. I thought I did everything correctly (creating references to the appropriate tables, changed the permitted values on Profile.create, and updated my schema), but I keep getting this error:

[1] pry(#<ProfilesController>)> Profile.new(profile_params)
ActionController::ParameterMissing: param is missing or the value is empty: profile
from /home/sdyer/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/strong_parameters.rb:310:in `require'
[2] pry(#<ProfilesController>)> profile_params
ActionController::ParameterMissing: param is missing or the value is empty: profile
from /home/sdyer/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/strong_parameters.rb:310:in `require'

But I'm passing all the right paramters to the create function.

[1] pry(#<ProfilesController>)> params
=> <ActionController::Parameters {"credentials"=>{"email"=>"testA", "password"=>"test", "password_confirmation"=>"test"}, "menu_item"=>{"price"=>"9.99", "name"=>"Pizza", "description"=>"Saucy goodness", "location"=>"200 Blue St, Cat City"}, "coupon_menu"=>{"price"=>"9.99", "name"=>"Pizza", "description"=>"Saucy goodness", "location"=>"200 Blue St, Cat City"}, "controller"=>"profiles", "action"=>"create", "format"=>"json"} permitted: false>

Here are the related files: Profile Model

class Profile < ApplicationRecord
  belongs_to :user
  belongs_to :menu_item
  belongs_to :coupon_menus
end

Profile Controller (changed to applicationController so I could post requests w/o token)

# class ProfilesController < OpenReadController
class ProfilesController < ApplicationController

  before_action :set_profile, only: [:show, :update, :destroy]
#  before_action :all_profiles, only: [:show_user]
  # GET /profiles
  def index
    @profiles = current_user.profiles

    render json: @profiles
  end

  # GET /profiles/1
  def show
    render json: @profile.user
  end

  # def show_profiles
  #   @profiles = []
  #   Profile.find_each do |profile|
  #     if profile.user.id ==
  #       @profiles.push profile.menu_item
  #     end
  #   end
  #   render json: @profiles
  # end

  # GET /profiles/show-profile-items/1
  def show_profile_total
    @menu = []
    MenuItem.find_each do |item|
      if item.price < params[:price].to_i
        @menu.push item
      end
    end
    render json: @menu
  end

  # GET /profiles/show-profile-items/1&1
  def show_profile_items
    @profiles = []
    index = params[:menu].to_i
    puts index
    Profile.find_each do |profile|
      if profile.user.id == params[:id].to_i && profile.menu_item.id == index
        @profiles.push profile.menu_item
      end
    end
    render json: @profiles
  end

  # POST /profiles
  def create
    binding.pry
    @profile = Profile.new(profile_params)

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

  # PATCH/PUT /profiles/1
  def update
  #  binding.pry
      @profile.menu_item.price = 0
      @profile.menu_item.save(validate: true)
      render json: @profile
    # else
    #   render json: @profile.errors, status: :unprocessable_entity
    # end
  end

  # DELETE /profiles/1
  def destroy
    @profile.destroy
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_profile
      @profile = current_user.profiles.find(params[:id])
    end

    # def all_profiles
    #   Profile.where(params[:id]).each |x| do
    #     @profiles_array.push(x)
    #   end
    # end

    # Only allow a trusted parameter "white list" through.
    def profile_params
      binding.pry
      params.require(:profile).permit(:user_id, :menu_item_id, :coupon_menu_id)
    end
end

Schema


ActiveRecord::Schema.define(version: 20170623022140) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "coupon_menus", force: :cascade do |t|
    t.integer  "price"
    t.string   "name"
    t.string   "description"
    t.string   "location"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "examples", force: :cascade do |t|
    t.text     "text",       null: false
    t.integer  "user_id",    null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_examples_on_user_id", using: :btree
  end

  create_table "menu_items", force: :cascade do |t|
    t.integer  "price"
    t.string   "name"
    t.string   "description"
    t.string   "location"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "profiles", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "menu_item_id"
    t.integer  "coupon_menu_id"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
    t.index ["menu_item_id"], name: "index_profiles_on_menu_item_id", using: :btree
    t.index ["user_id"], name: "index_profiles_on_user_id", using: :btree
    t.index ["coupon_menu_id"], name: "index_profiles_on_coupon_menu_id", using: :btree
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",           null: false
    t.string   "token",           null: false
    t.string   "password_digest", null: false
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    t.index ["token"], name: "index_users_on_token", unique: true, using: :btree
  end

  add_foreign_key "examples", "users"
  add_foreign_key "profiles", "menu_items"
  add_foreign_key "profiles", "users"
  add_foreign_key "profiles", "coupon_menus"
end
jordanallain commented 7 years ago

can you post the ajax/curl request of posting to profiles?

slammyde7113 commented 7 years ago
#!/bin/bash
curl --include --request POST http://localhost:4741/profiles \
--header "Content-Type: application/json" \
--data '{
  "credentials": {
    "email": "testA",
    "password": "test",
    "password_confirmation": "test"
  },
  "menu_item": {
    "price": "9.99",
    "name": "Pizza",
    "description": "Saucy goodness",
    "location": "200 Blue St, Cat City"
  },
  "coupon_menu": {
    "price": "9.99",
    "name": "Pizza",
    "description": "Saucy goodness",
    "location": "200 Blue St, Cat City"
  }
}'
slammyde7113 commented 7 years ago

actually, wouldn't be easier to just make another column in menu_item for coupon price? then I wouldnt have to make a new table

slammyde7113 commented 7 years ago

I'm gonna do that instead