2016-09-10 61 views
0

我编写了一个android应用程序,它可以从用户获取个人资料图片。现在我想将这些个人资料图片上传到我的Ruby on Rail服务器上。但是,上传不起作用。我收到错误消息:ActionController :: ParameterMissing(param丢失或值为空:item):

app/controllers/items_controller.rb:55:in `item_params' 
    app/controllers/items_controller.rb:21:in `create' 
    Started POST "/items" for 192.168.3.7 at 2016-09-11 01:12:21 +0900 
    Processing by ItemsController#create as HTML 
     Parameters: {"image"=>#<ActionDispatch::Http::UploadedFile:0x5737110 @tempfile=#<Tempfile:C:/Users/Clemens/AppData/Local/Temp/RackMultipart20160911-2 
    8624-1vjbftr.jpg>, @original_filename="IMG_20160911_010525.jpg", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; na 
    me=\"image\"; filename=\"IMG_20160911_010525.jpg\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n">} 
    Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms) 

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

app/controllers/items_controller.rb:55:in `item_params' 
app/controllers/items_controller.rb:21:in `create' 

为什么这不起作用?我的item_params应该如何定义?这里是我的items_controller.rb

class ItemsController < ApplicationController 
    before_action :set_item, only: [:show, :edit, :update, :destroy] 

    # GET /items 
    # GET /items.json 
    def index 
    @items = Item.all 
    end 

    # GET /items/1 
    # GET /items/1.json 
    def show 
    send_data(item.file_contents, 
       type: @item.content_type, 
       filename: @item.filename) 
    end 

    # POST /items 
    # POST /items.json 
    def create 
    @item = Item.new(item_params) 

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

    # PATCH/PUT /items/1 
    # PATCH/PUT /items/1.json 
    def update 
    if @item.update(item_params) 
     render :show, status: :ok, location: @item 
    else 
     render json: @item.errors, status: :unprocessable_entity 
    end 
    end 

    # DELETE /items/1 
    # DELETE /items/1.json 
    def destroy 
    @item.destroy 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_item 
     @item = Item.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def item_params 
     #######################params.require(:item).permit(:name, :description, :picture) 
     params.permit(:picture) 
    end 
end 

更新:我从Android应用程序改名为我的图像,并把它称为“项目”。现在参数错误消失。然而,出现新的错误:

app/controllers/items_controller.rb:24:in `create' 
Started POST "/items" for 192.168.3.7 at 2016-09-11 10:25:26 +0900 
Processing by ItemsController#create as HTML 
    Parameters: {"item"=>#<ActionDispatch::Http::UploadedFile:0x53b1d30 @tempfile=#<Tempfile:C:/Users/Clemens/AppData/Local/Temp/RackMultipart20160911-3144-anlpp6.jpg>, @original_filename="IMG_20160911_100920.jpg", @co 
ntent_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"item\"; filename=\"IMG_20160911_100920.jpg\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n" 
>} 
Unpermitted parameter: item 
    (0.0ms) BEGIN 
    SQL (31.2ms) INSERT INTO `items` (`created_at`, `updated_at`) VALUES ('2016-09-11 01:25:26', '2016-09-11 01:25:26') 
    (0.0ms) COMMIT 
Completed 500 Internal Server Error in 62ms (ActiveRecord: 31.2ms) 



ActionView::MissingTemplate (Missing template items/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: 
    * "C:/Benutzer/Clemens/RubymineProjects/rails-api-fileupload-tutorial-carrierwave-single/app/views" 
): 

app/controllers/items_controller.rb:24:in `create' 

任何想法,为什么我得到这个错误?我把意见如下行无论是在意见\程序\ show.json.jbuilder和意见\项目\ show.json.jbuilder:

json.extract! @item, :locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder] 

但是我仍然得到同样的错误。

+0

'params.require(:item).permit(:name,:description,:picture)'为什么这个被注释掉了? – neo

+0

我很确定这不是当时他看到那个错误信息! :) –

回答

2

正在发生的事情是,这条线:

params.require(:item).permit(:name, :description, :picture) 

将引发异常,如果你没有在你的PARAMS的项目。当你发送到创建动作时,显然你没有物品。我还假设,当你看到这个错误时,你没有那条评论。

+0

你的假设是正确的。在我为这个错误感到惊讶之后,我评论了这一行,因为我只是发送图像而不是名称或描述。所以我需要重新命名我的Android应用程序发送的图像,称之为“item”,问题就解决了吗? – Peter

+0

我将图像重命名为“条目”,请参阅我的文章中的更新。我的参数仍然有问题。我不知道如何红宝石轨道上可以使用从android发送的参数。我不知道该怎么办 – Peter

+0

商品与商品不一样。您可以发送名为item的参数,其中包含3个字段:名称,说明和图片。项目上的任何其他字段或任何参数不称为项目是直出。 –

相关问题