Rails:param缺失或值为空:stock

时间:2017-05-22 17:05:17

标签: ruby-on-rails ruby cloud9-ide cloud9

当我尝试为在StocksController中创建ActionController :: ParameterMissing的数据库创建新项目时,我一直收到错误#create param缺失或值为空:stock。

它说来源是

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

在我的终端上的stockcontroller文件中,它说:

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'

我基本上重复了一个类似于我已经创建的代码的代码,但我无法找到为什么这是错误的。我有2种类型的数据库,供应商和股票,但股票数据库不起作用。

这是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

新/ stock / 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 %> 

模型代码

class Stock < ApplicationRecord
end

我的路线/ locals / config / app文件

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

我的架构/ 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

新错误说:

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

app/controllers/stocks_controller.rb上的来源:

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

    def index

终端说:

ActiveRecord::RecordNotFound (Couldn't find Stock with 'id'=5):
app/controllers/stocks_controller.rb:3:in `show'

2 个答案:

答案 0 :(得分:0)

你没有在你的参数中返回:stock密钥。

更改

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

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

答案 1 :(得分:0)

这似乎是一个复数与sigular问题。

您的代码需要stock参数,但它已经stocks

尝试更改此行: <%= form_for :stocks, url: stocks_path do |f| %>

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