ユーザーからプロファイル写真を取得できるアンドロイドアプリをプログラミングしました。これらのプロフィール写真を私のRuby on Railサーバーにアップロードします。ただし、アップロードは機能しません。エラーメッセージ:ActionController :: ParameterMissing(パラメータがないか値が空です: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
UPDATEです:私は、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'
なぜこのエラーが発生するのですか?私はまだ同じエラーを取得しかし
json.extract! @item, :locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]
:私はshow.json.jbuilder \アプリケーションの\ show.json.jbuilderと項目\ビューで\ビュー内の両方の行を次のビューに置きます。あなたのparams内のアイテムを持っていない場合
params.require(:item).permit(:name, :description, :picture)
例外が発生します:何が起こっている
'params.require(:item).permit(:name、:description、:picture)'なぜこれはコメントアウトされていますか? – neo
私はそれがそのエラーメッセージを見た時ではなかったと確信しています! :) –