ActionController :: ParameterMissing(param丢失或值为空:item):

时间:2016-09-10 16:27:21

标签: ruby-on-rails file-upload

我编写了一个Android应用程序,可以从用户那里获取个人资料图片。现在我想将这些个人资料图片上传到我的Ruby on Rail服务器。但是上传不起作用。我收到错误消息:

[:TO]

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

  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'

更新:我从Android应用程序重命名了我的图像并将其命名为“item”。现在参数错误消失了。然而,出现了新的错误:

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

知道我为什么会收到这个错误吗?我在views \ application \ show.json.jbuilder和views \ items \ show.json.jbuilder中输入了以下视图:

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'

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

1 个答案:

答案 0 :(得分:2)

这一行正在发生:

params.require(:item).permit(:name, :description, :picture)
如果您的参数中没有项目,

将引发异常。当您发送到创建操作时,显然您没有项目。我也假设当你看到这个错误时,你没有评论该行。