Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

ruby on rails - Nested Resources id param

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):


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you create an item you do a POST request to the path: /api/v1/users/:user_id/categories/:category_id/items

So the url wil look something like: /api/v1/users/1/categories/1/items

Then I guess the params that you receive are: user_id and category_id and you are missing the param ID that you are looking for. You should check the logs for what params you receive and adjust your code to pick the right param.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...