2015-06-18 42 views
0

items_controller.rbPARAM缺失或为空值:项

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}(#{[email protected]_record?})" 
    end 

end 

错误:PARAM缺失或为空值:项

Rails.root:E:/ work/my_store_2

Application Trace |框架跟踪|全面跟踪 应用程序/控制器/ items_controller.rb:9:'创建”请求

参数:

{ “名”=> “CAR 1”, “说明”=> “好车”,“价格“=>” 500000" , “重”=> “0”, “真实”=> “1”}

控制台

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

请张贴您的路线...创建应该是发布,而不是得到 – gabrielhilal

回答

1

当你做params.require(:item)它需要item参数存在。所以,create行动期待这样的参数:{ "item" => {"name"=>"car1", "description"=>"good car", "price"=>"500000", "weight"=>"0", "real"=>"1"} }

您错过了item根密钥。

1

您错过了表单上的“项目”。添加id =“item”的字段。

相关问题