중첩 된 리소스 경로를 구축하려고합니다.
먼저 사용자를 생성하고 작업 의지를 생성하고 사용자 카테고리 컨트롤러를 생성하고 항목 컨트롤러를 생성하지만 항목을 생성 할 때 문제가 있습니다.
첫 번째 카테고리 컨트롤러
class Api::V1::CategoriesController < ApplicationController
before_action :authenticate_with_token!, only: [:create, :update, :destroy]
before_action :set_category, only: [:show, :update, :destroy]
# GET /categories
def index
@categories = current_user.category.all
render json: @categories
end
# GET /categories/1
def show
render json: @category
end
# POST /categories
def create
@category = current_user.categories.new(category_params)
if @category.save
render json: @category, status: :created
else
render json: @category.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /categories/1
def update
if @category.update(category_params)
render json: @category
else
render json: @category.errors, status: :unprocessable_entity
end
end
# DELETE /categories/1
def destroy
@category.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_category
@category = current_user.category.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def category_params
params.require(:category).permit(:title)
end
end
두 번째 항목 컨트롤러
class Api::V1::ItemsController < ApplicationController
before_action :authenticate_with_token!, only: [:create, :update, :destroy]
before_action :set_item, only: [:show, :update, :destroy]
# GET /items
def index
@items = current_type.items.all
render json: @items
end
# GET /items/1
def show
render json: @item
end
# POST /items
def create
@item = current_type.item.new(item_params)
if @item.save
render json: @item, status: :created, location: @item
else
render json: @item.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /items/1
def update
if @item.update(item_params)
render json: @item
else
render json: @item.errors, status: :unprocessable_entity
end
end
# DELETE /items/1
def destroy
@item.destroy
@item.image.purge
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
@item = current_type.item.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def item_params
params.require(:item).permit(:title, :price, :image)
end
end
애플리케이션 컨트롤러
class ApplicationController < ActionController::API
def current_user
@current_user ||= User.find_by(auth_token: request.headers['Authorization'])
end
def authenticate_with_token!
render json: { errors: "Not authenticated" },status: :unauthorized unless user_signed_in?
end
def user_signed_in?
current_user.present?
end
def prepare_user
@user = User.find(params[:id])
end
def current_type
@current_type ||= Type.find(params[:id])
end
end
내 경로
Rails.application.routes.draw do
devise_for :users
namespace :api do
namespace :v1 do
resources :sessions, :only => [:create, :destroy]
resources :users, :only => [:show, :create, :update, :destroy] do
resources :categories do
resources :items
end`
end
end
end
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
ActiveRecord :: RecordNotFound 항목을 만들려고 할 때 (ID없이 유형을 찾을 수 없음) :
항목을 생성하는 경우 POST request
경로에 대한 작업을 수행합니다 ./api/v1/users/:user_id/categories/:category_id/items
따라서 URL은 다음과 같습니다. /api/v1/users/1/categories/1/items
그런 다음받는 매개 변수는 다음 user_id and category_id
과 같습니다 . 찾고있는 매개 변수 ID가 누락되었습니다. 수신 한 매개 변수에 대한 로그를 확인하고 올바른 매개 변수를 선택하도록 코드를 조정해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다