I am trying to build Nested Resources Route
First I generate user it's work will and create a user category controller and create item controller but I have a problem when I create an item
First category controller
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
second item controller
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
Application controller
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
my routes
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
when I try to create the item
ActiveRecord::RecordNotFound (Couldn't find Type without an ID):