param丢失或值为空:item

时间:2015-06-18 18:34:13

标签: ruby-on-rails ruby

items_controller.rb

class ItemsController < ApplicationController

  def index
    @items = Item.all
    render text: @items.map{ |i| "#{i.name}: #{i.price}" }.join('<br/>')
  end

  def create
    item_params = params.require(:item).permit(:name, :description, :price, :real, :weight)
    @item = Item.create(item_params)
    render text: "#{@item.id}: #{@item.name}(#{!@item.new_record?})"
  end

end
  

错误:缺少参数或值为空:item

     

Rails.root:E:/ work / my_store_2

     

应用程序跟踪|框架跟踪|完整追踪   app / controllers / items_controller.rb:9:在'create&#39;请求

     

参数:

     

{&#34; name&#34; =&gt;&#34; car1&#34;,&#34;说明&#34; =&gt;&#34;好车&#34;,&#34;价格& #34; = GT;&#34; 500000&#34 ;,   &#34;体重&#34; =&gt;&#34; 0&#34;,&#34;真实&#34; =&gt;&#34; 1&#34;}

控制台

Started GET "/items/create?name=car1&description=good+car&price=500000&weight=0&real=1" for 127.0.0.1 at 2015-06-18 21:25:39 +0300
Processing by ItemsController#create as HTML
  Parameters: {"name"=>"car1", "description"=>"good car", "price"=>"500000", "weight"=>"0", "real"=>"1"}
Completed 400 Bad Request in 2ms

ActionController::ParameterMissing (param is missing or the value is empty: item):
  app/controllers/items_controller.rb:9:in `create'

我的错误在哪里?

2 个答案:

答案 0 :(得分:1)

执行params.require(:item)时,需要item参数存在。因此,create操作期望像这样的参数:{ "item" => {"name"=>"car1", "description"=>"good car", "price"=>"500000", "weight"=>"0", "real"=>"1"} }

您缺少item根密钥。

答案 1 :(得分:1)

您错过了&#39;项目&#39;在你的表格上。添加id =&#34; item&#34;。

的字段