1
我在我的應用程序中設置了外部API控制器。一切正常。ruby on rails,多對多json api
其中一個控制器是groupings_controller.rb。 分組是用戶和組之間的多對多關係表。
class Api::V1::GroupingsController < ApplicationController
def create
@group = Group.find_by_id(params[:grouping][:group_id])
@grouping.current_user = current_user
@grouping = @group.groupings.build(user_id: current_user.id, group_id: @group.id)
@grouping.user_id = current_user.id
respond_to do |format|
if @grouping.save
format.html { redirect_to @grouping, notice: 'Grouping was successfully created.' }
format.json { render json: @grouping, status: :created, location: @grouping }
else
format.html { render action: "new" }
format.json { render json: @grouping.errors, status: :unprocessable_entity }
end
end
end
然而,當我創建分組
curl -v -H 'Content-Type: application/json' -H 'Accept: application/vnd.app.v1' -X POST http://localhost:3000/api/groupings/\?auth_token\=SH5nwhDSbsomyQCZzb8T -u "admin:secret" -d "{\"grouping\":{\"group_id\":\"1\"}}"
我得到響應
{"grouping":{"id":52,"user_id":null,"group_id":1}}*
看起來它創建了非API控制器分組。如果我從非api控制器刪除了創建,服務器會拋出錯誤。
APP::Application.routes.draw do
namespace :api, defaults: { format: 'json' } do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
resources :groupings
end
end
resources :groupings
end