我正在使用产品模型运行一个仅使用Rails Api-Apply应用程序。我已经使用ActiveAdmin在后端创建了产品。当使用Postman查看具有相应产品ID的单个产品时,它会返回JSON数据罚款,但是当我尝试查看所有产品时:

问题描述 投票:0回答:1

启动“/api/v1/products/” for :: 1 at 2019-05-28 11:12:47 +0100 由API :: V1 :: ProductsController#index作为JSON处理 参数:{“ product” => {}} 过滤链停止为:set_product渲染或重定向 在5ms中找不到404个(视图:0.2ms | ActivereCord:4.9ms)

这里是我的控制器:

module Api module V1 class ProductsController < Api::V1::ApiController # do not authenticate users before viewing product listing or individual products before_action :authenticate_user!, except: %i[index show] before_action :set_product, only: %i[index show] # GET /product def index product = Product.all.order('id') render json: { success: true, data: product }, status: :ok end # GET /products/:id def show @product = Product.find(params[:id]) render json: { success: true, data: @products }, status: :ok end private def set_product @products = Product.find(params[:id]) rescue StandardError => error render json: { success: false, error: error }, status: 404 end def product_params params.require(:product).permit(:title, :release_date, :release_time, :description, :price, :brand) end end end end
我的API观点是:

__PRODUCT.JSON.JBUILDER

json.id product.id json.title product.title json.description product.description json.release_date product.release_date json.release_time product.release_time json.price product.price json.brand product.brand

Index.json.jbuilder

json.array! @products, partial: 'products/product', as: :product

show.json.jbuilder

json.product do json.partial! 'product' end

routes

namespace :api do namespace :v1, defaults: { format: :json } do devise_scope :user do get :status, to: 'api#status' resource :user, only: %i[update show] do get :profile end end resources :products, only: [:index, :show] end end

Convert

module Api module V1 class ProductsController < Api::V1::ApiController # do not authenticate users before viewing product listing or individual products before_action :authenticate_user!, except: %i[index show] before_action :set_product, only: %i[index show] # GET /product def index product = Product.all.order('id') render json: { success: true, data: product }, status: :ok end # GET /products/:id def show @product = Product.find(params[:id]) render json: { success: true, data: @products }, status: :ok end private def set_product @products = Product.find(params[:id]) rescue StandardError => error render json: { success: false, error: error }, status: 404 end def product_params params.require(:product).permit(:title, :release_date, :release_time, :description, :price, :brand) end end end end

to
module Api
  module V1
    class ProductsController < Api::V1::ApiController
      # do not authenticate users before viewing product listing or individual products
      before_action :authenticate_user!, except: %i[index show]
      before_action :set_product, only: %i[show]

      # GET /product
      def index
        @products = Product.all.order('id')
        render json: { success: true, data: @products }, status: :ok
      end

      # GET /products/:id
      def show
        if @product.present?
          render json: { success: true, data: @product }, status: :ok
        else
           render json: { success: false }, status: :not_found
        end

      end

      private

      def set_product
        @product = Product.find(params[:id])
      end

      def product_params
        params.require(:product).permit(:title, :release_date, :release_time, :description, :price, :brand)
      end

    end
  end
end
ruby-on-rails ruby postman ruby-on-rails-5
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.