JSON文字列でHTTP POSTリクエストを受信し、JSONオブジェクトをデータベースに追加できるはずのRuby on Railsサーバーアプリケーション(Herokuでホスト)があります。感謝祭とリクエストの 2 つのデータベース モデルがあります。
コントローラー/request_controller.rb:
class RequestsController < ApplicationController
[...]
# POST /requests
# POST /requests.json
def create
@request = Request.new(params[:request])
respond_to do |format|
if @request.save
format.html { redirect_to @request, notice: 'Request was successfully created.' }
format.json { render json: @request, status: :created, location: @request }
else
format.html { render action: "new" }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
[...]
end
さまざまなデータベース モデル オブジェクトの値の JSON キーは、感謝祭 DB モデルと要求 DB モデルに対して、それぞれ「thanks」と「request」である必要があります。
モデル/request.rb:
class Request < ActiveRecord::Base
attr_accessible :request
end
モデル/感謝祭.rb:
class Thanksgiving < ActiveRecord::Base
attr_accessible :thanks
end
http://YADA.herokuapp.com/thanksgivings.jsonに HTTP リクエストを送信するとすべて正常に動作しますが、.../requests.json に送信すると 500 エラーが発生します。JSON 文字列で誤ったキーを使用すると、リクエストは成功しますが、値が null になります。
リクエスト 1: (thanksgiving.json への正しいリクエスト)
POST
Host: http://YADA.herokuapp.com/thanksgivings.json
Content-Type: application/json
Body: {"thanks":"qwerty"}
リクエスト 2: (request.json への正しいリクエスト – 500 を返します)
POST
ホスト: http://YADA.herokuapp.com/requests.json
コンテンツ タイプ: application/json
本文: {"request":"qwerty"}
リクエスト 3: (障害のあるキー - 成功: 値は null になり、応答でキーは「リクエスト」になり、データベース要素が追加されます)
POST
ホスト: http://YADA.herokuapp.com/requests.json
コンテンツ タイプ: application/json
本文: {"abc":"qwerty"}
奇妙なことに、2 つのコントローラー、thanksgivings_controller.rb と requests_controller.rb は「同一」ですが、動作が異なります。
これを成功させる方法を知っている人はいますか?