I'm running a Rails api-only app with a product model. I've created products on the backend with activeadmin. When using postman to view individual products with a corresponding product ID, it returns the json data fine but when I try to view all product at: api/v1/products
I get this error:
Started GET "/api/v1/products/" for ::1 at 2019-05-28 11:12:47 +0100Processing by Api::V1::ProductsController#index as JSONParameters: {"product"=>{}}Filter chain halted as :set_product rendered or redirectedCompleted 404 Not Found in 5ms (Views: 0.2ms | ActiveRecord: 4.9ms)
Here's my controller:
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 endend
Here are my API views:
_product.json.jbuilder
json.id product.idjson.title product.titlejson.description product.descriptionjson.release_date product.release_datejson.release_time product.release_timejson.price product.pricejson.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