2
私は自分のサーバーで送信しているJSONリクエストからデータを保存しようとしています。私はすでにCSRFディフェンダーに例外を追加し、リクエストを渡します。 ActiveRecordsはデータを保存しませんが、HTMLリクエストではそのようにします。Rails 4:ActiveRecordsはJSONからデータを保存できません
私はJSONを生成するカールを使用するには、
curl -v -X POST -d '{"claim": {"lastname":"Jhon Smith","phone":"+1(51)555555","latitude":"10.4","longitude":"12.7","theme":"Test message","text":"Text of test message"}}' -H "Content-Type:application/json" -H "Accept:application/json" http://localhost:3000/claims/new
それはメッセージコードを返す200
> POST /claims/new HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.43.0
> Content-Type:application/json
> Accept:application/json
> Content-Length: 167
>
* upload completely sent off: 167 out of 167 bytes
< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: application/json; charset=utf-8
< Cache-Control: no-cache
を要求したサーバは、データを捕まえたが何もしなかったと答えました。
Started POST "/claims/new" for 127.0.0.1 at 2016-04-16 01:35:24 +0300
Processing by ClaimsController#new as JSON
Parameters: {"claim"=>{"lastname"=>"Jhon Smith", "phone"=>"+1(51)555555", "latitude"=>"10.4", "longitude"=>"12.7", "theme"=>"Test message", "text"=>"Text of test message"}}
Rendered claims/new.json.erb (0.0ms)
Completed 200 OK in 2ms (Views: 1.2ms | ActiveRecord: 0.0ms)
JSON保存しようとする私のコントローラのコードがあります:私はのために感謝するでしょうルータ
post 'claims/new' => 'claims#new'
で次の設定を経由してPOSTにアクセス
def create
@claim = Claim.new(claim_params)
respond_to do |format|
if @claim.save
format.html do
redirect_to acceptedclaim_path
flash[:success] = "Ваша заява прийнята! Дякуємо за допомогу!"
end
format.json do
render json: @claim, status: :created, location: @claim
end
else
format.html do
redirect_to new_claim_path
flash[:danger] = flash_errors(@claim)
end
format.json do
render json: @claim.errors, status: :unprocessable_entity
end
end
end
end
def claim_params
params.require(:claim).permit(:lastname, :phone, :latitude, :longitude, :theme, :text)
end
は、助けて!
ありがとうございました!できます! – Ic2h