我编写了一个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]
但是我仍然得到同样的错误。
'params.require(:item).permit(:name,:description,:picture)'为什么这个被注释掉了? – neo
我很确定这不是当时他看到那个错误信息! :) –