[英]Why am I getting a “Completed 406 Not Acceptable” when a POST request is made in my Rails 5 app?
有點背景。 我正在創建一個由外部服務器聯系的API。 該服務器使用唯一的ID號向端點/車輛發送POST請求來聯系API。 請求主體的格式為{“ id”:“ some_string_here”}。 我的API將這些存儲在db中。 在創建車輛之后,外部服務器將使用端點車輛/:id /位置發出POST請求,請求主體看起來像這樣{{“ lat”:23.0,“ lng”:30.0,“ at”:“ 2018- 09-02T07:00:00Z“}-定期向每輛車添加經緯度坐標和時間戳(以更新車的位置)。
我已經能夠使用車輛和位置控制器設置我的API,並且當我啟動外部服務器時,車輛已成功存儲。 我遇到的問題是服務器何時開始發送POST請求以將位置添加到每輛車。 我得到錯誤:
Started POST "/vehicles/a38edd77-07b0-4069-8a1d-462428989ecf/locations" for 127.0.0.1 at 2019-07-29 13:53:21 +0200
Processing by LocationsController#create as HTML
Parameters: {"lat"=>52.44339, "lng"=>13.24326, "at"=>"2019-07-29T11:53:20.044Z", "vehicle_id"=>"a38edd77-07b0-4069-8a1d-462428989ecf", "location"=>{"lat"=>52.44339, "lng"=>13.24326, "at"=>"2019-07-29T11:53:20.044Z"}}
Vehicle Load (0.9ms) SELECT "vehicles".* FROM "vehicles" WHERE "vehicles"."unique_id" = $1 LIMIT $2 [["unique_id", "a38edd77-07b0-4069-8a1d-462428989ecf"], ["LIMIT", 1]]
↳ app/controllers/locations_controller.rb:10
(0.7ms) BEGIN
↳ app/controllers/locations_controller.rb:15
(2.4ms) ROLLBACK
↳ app/controllers/locations_controller.rb:15
Completed 406 Not Acceptable in 12ms (Views: 0.2ms | ActiveRecord: 4.0ms)
ActionController::UnknownFormat (ActionController::UnknownFormat):
知道什么可以解決此問題嗎? 任何幫助將非常感激。 我的代碼如下。
控制器/ vehicles_controller.rb:
class VehiclesController < ApplicationController
def index
@vehicles = Vehicle.all
render json: @vehicles
end
def create
@vehicle = Vehicle.new(vehicle_params)
respond_to do |format|
if @vehicle.save
render json: @vehicle, status: :created, location: @vehicle
else
render json: @vehicle.errors, status: :unprocessable_entity
end
end
end
def destroy
@vehicle.destroy
respond_to do |format|
format.json { head :no_content }
end
end
private
def vehicle_params
# swap id column value with unique_id to avoid conflict with primary key
params[:vehicle][:unique_id] = params[:vehicle].delete(:id)
params.require(:vehicle).permit(:unique_id)
end
end
控制器/ locations_controller.rb
class LocationsController < ApplicationController
def index
@locations = Location.all
render json: @locations
end
def create
@vehicle = Vehicle.find_by!(unique_id: params[:vehicle_id])
@location = @vehicle.locations.new(location_params)
respond_to do |format|
if @location.save
render json: @location, status: :created, location: @location
else
render json: @location.errors, status: :unprocessable_entity
end
end
end
private
def location_params
params.require(:location).permit(:lat, :lng, :at, :vehicle_id)
end
end
vehicle.rb
class Vehicle < ApplicationRecord
has_many :locations
end
location.rb
class Location < ApplicationRecord
belongs_to :vehicles
end
的routes.rb
Rails.application.routes.draw do
resources :vehicles do
resources :locations
end
end
schema.rb
ActiveRecord::Schema.define(version: 2019_07_27_224818) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "locations", force: :cascade do |t|
t.float "lat"
t.float "lng"
t.datetime "at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "vehicle_id"
end
create_table "vehicles", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "unique_id"
end
end
只要您的服務器以API身份運行,就應該跳過CSRF令牌真實性驗證
class LocationsController < ApplicationController
skip_before_action :verify_authenticity_token
...
而且似乎你在關系方面有錯誤
class Location < ApplicationRecord
belongs_to :vehicles <-- should be :vehicle here
end
嘗試使用save!
而不是save
,看看有什么錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.