0
votes

I keep getting an error when i try to create a new item for a database that displays ActionController::ParameterMissing in StocksController#create param is missing or the value is empty: stock.

It says the source is at

private 
 def stock_params
    params.require(:stock).permit(:product_name, :brand_name, :item_id, :upc_code, :color, :department, :size, :condition, :fabric_type, :shipping_weight, :sku, :asin, :quantity, :cost_price, :sell_price, :key_product_features,  :product_description, :search_terms, :status, :listing_in_usa, :listing_in_canada, :listing_in_mexico)   
end

Under my stockscontroller file on the terminal it says:

Started POST "/stocks" for 107.15.253.59 at 2017-05-22 16:38:14 +0000
Cannot render console from 107.15.253.59! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by StocksController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"poJrp+PWBi7bxgeauJ37VNh/Shyk0q6iCNA+5Y4UBIZ/rf5dXYVBOgtPpVPUbfzjLsn9Kcz/Fn6kHKXHG0dUuQ==", "stocks"=>{"product_name"=>"", "brand_name"=>"", "item_id"=>"", "upc_code"=>"", "color"=>"", "department"=>"", "size"=>"", "condition"=>"", "fabric_type"=>"", "shipping_weight"=>"", "sku"=>"", "asin"=>"", "quantity"=>"", "cost_price"=>"", "sell_price"=>"", "key_product_feature"=>"", "product_description"=>"", "search_terms"=>"", "status"=>"", "listing_in_usa"=>"", "listing_in_canada"=>"", "listing_in_mexico"=>""}, "commit"=>"Save Stocks"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: stock):

app/controllers/stocks_controller.rb:48:in `stock_params'
app/controllers/stocks_controller.rb:15:in `create'

I basically repeated a code that is similar to this that works that i already created, but i can't find why this is wrong. I have 2 types of databases, Vendors and Stocks, but the Stocks database isn't working.

This is the code for stockscontroller/concerns/controllers/apps

class StocksController < ApplicationController
def show
    @stock = Stock.find(params[:id])
end

def index
    @stocks = Stock.all
end

def new
    @stock = Stock.new
end

def create
    @stock = Stock.new(stock_params)


    if @stock.save
        redirect_to @stock
    else
        render 'new'
    end
end

def edit
    @stock = Stock.find(params[:id])
end

def update
    @stock = Stock.find(params[:id])
    if @stock.update(stock_params)
        redirect_to @stock
    else
        render 'edit'
    end
end

def destroy
    @stock = Stock.find(params[:id])
    @stock.destroy

    redirect_to stock_path
end
end

private 
 def stock_params
    params.require(:stock).permit(:product_name, :brand_name, :item_id, :upc_code, :color, :department, :size, :condition, :fabric_type, :shipping_weight, :sku, :asin, :quantity, :cost_price, :sell_price, :key_product_features,  :product_description, :search_terms, :status, :listing_in_usa, :listing_in_canada, :listing_in_mexico)   
end

code for new/stocks/views/apps

<body>
    <div class = "head5">
        <h1>New Inventory</h1>

        <div class = "header5">
            <h3>  Apparel Inventory</h3>
        </div>
    </div>

</body>
<%= form_for :stocks, url: stocks_path do |f| %>

<p>
    <%= f.label :product_name %><br>
   <%= f.text_field :product_name %>
</p>

<p>
    <%= f.label :brand_name %><br>
    <%= f.text_field :brand_name %>
</p>

<p>
    <%= f.label :item_id %><br>
    <%= f.text_field :item_id %>
</p>

<p>
    <%= f.label :upc_code %><br>
    <%= f.text_field :upc_code %>
</p>

<p>
    <%= f.label :color %><br>
    <%= f.text_field :color %>
</p>


<p>
    <%= f.label :department %><br>
    <%= f.text_field :department %>
</p>

<p>
    <%= f.label :size %><br>
    <%= f.text_field :size %>
</p>

<p>
    <%= f.label :condition %><br>
    <%= f.text_field :condition %>
</p>

<p>
    <%= f.label :fabric_type %><br>
    <%= f.text_field :fabric_type %>
</p>

<p>
    <%= f.label :shipping_weight %><br>
    <%= f.text_field :shipping_weight %>
</p>

<p>
    <%= f.label :sku %><br>
    <%= f.text_field :sku %>
</p>

<p>
    <%= f.label :asin %><br>
    <%= f.text_field :asin %>
</p>

<p>
    <%= f.label :quantity %><br>
    <%= f.text_field :quantity %>
</p>

<p>
    <%= f.label :cost_price %><br>
    <%= f.text_field :cost_price %>
</p>

<p>
    <%= f.label :sell_price %><br>
    <%= f.text_field :sell_price %>
</p>

<p>
    <%= f.label :key_product_feature %><br>
    <%= f.text_field :key_product_feature %>
</p>

<p>
    <%= f.label :product_description %><br>
    <%= f.text_field :product_description %>
</p>

<p>
    <%= f.label :search_terms %><br>
    <%= f.text_field :search_terms %>
</p>

<p>
    <%= f.label :status %><br>
    <%= f.text_field :status %>
</p>

<p>
    <%= f.label :listing_in_usa %><br>
    <%= f.text_field :listing_in_usa %>
</p>

<p>
    <%= f.label :listing_in_canada %><br>
    <%= f.text_field :listing_in_canada %>
</p>

<p>
    <%= f.label :listing_in_mexico %><br>
    <%= f.text_field :listing_in_mexico %>
</p>

<p>
    <%= f.submit %>
</p>

<% end %>
<%= link_to 'Back', stocks_path %> 

Code for the model

class Stock < ApplicationRecord
end

my routes/locals/config/app file

Rails.application.routes.draw do
  get 'welcome/index'

  resources :vendors
  resources :stocks


  root 'welcome#index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

my schema/migrate/db/config/apps

ActiveRecord::Schema.define(version: 20170522154349) do
  create_table "stocks", force: :cascade do |t|
    t.string "product_name"
    t.string "brand_name"
    t.integer "item_id"
    t.integer "upc_code"
    t.string "color"
    t.string "size"
    t.string "department"
    t.string "condition"
    t.string "fabric_type"
    t.string "shipping_weight"
    t.string "sku"
    t.string "asin"
    t.integer "quantity"
    t.string "cost_price"
    t.string "sell_price"
    t.string "key_product_feature"
    t.text "product_description"
    t.text "search_terms"
    t.string "status"
    t.string "listing_in_usa"
    t.string "listing_in_canada"
    t.string "listing_in_mexico"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "vendors", force: :cascade do |t|
    t.string "company"
    t.string "contact_name"
    t.string "phone"
    t.string "email"
    t.string "moq"
    t.string "cost_per_item"
    t.string "payment_method"
    t.string "terms"
    t.string "turnover"
    t.string "returns"
    t.text "notes"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

The new error says:

ActiveRecord::RecordNotFound in StocksController#show
Couldn't find Stock with 'id'=5

At the source on app/controllers/stocks_controller.rb:

class StocksController < ApplicationController
    def show
        @stock = Stock.find(params[:id])
    end

    def index

With the terminal saying:

ActiveRecord::RecordNotFound (Couldn't find Stock with 'id'=5):
app/controllers/stocks_controller.rb:3:in `show'
2
You're params are nested under the "stocks" key (plural), but you're controller is expecting "stock" (singular). Try changing form_for => :stockAdam Sheehan
Ok, thanks, but new problem. When I try to delete a record, I get another error. Gonna edit my question..Davidson
I have a new problem, editing my question for you guys to seeDavidson
What were you doing when it could not find the record? Were you running a test? I don't see any tests in the question anymore. I assume that there is no record with ID=5, as indicated in the error message, but I can't tell for sure if it's an error in the spec or somewhere else.tomf
This seems to be new question. If the solutions below answered your first question, you should mart one as answered. You can create a new question for your second separate issue.tomf

2 Answers

0
votes

You're not returning a :stock key in your params.

Change

<%= form_for :stocks, url: stocks_path do |f| %>

Into

<%= form_for :stock, url: stocks_path do |f| %>
0
votes

This seems to be a plural vs. sigular issue.

Your code is requiring a stock param, but it's getting stocks.

Try changing this line: <%= form_for :stocks, url: stocks_path do |f| %>

to

<%= form_for :stock, url: stocks_path do |f| %>