0

When I'm going to index action it outputs whole data from my model, as I understood when I'm going to show action it supposed to output only object depends on id param, but in my case it output the same data as it were in index action.

My active model serializer:

class CategorySerializer < ApplicationSerializer
    attributes :id, :name, :alias, :parent_category_id, :position, :menu, :status
    has_many :subcategories
    has_many :products
end

Controller:

module API
  module Store
    class CategoriesController < ApplicationController
      def index
        @categories = Category.all
        if params[:name]
          @categories = Category.find_by(name: params[:name])
        end
        puts @categories
        render json: @categories
      end
      def show
        @category = Category.find(params[:id])
        render json: @category
      end
    end
  end
end
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
p0ison
  • 1
  • When you send `params[:name]` or not? It looks like the index is acting as a show if you pass `params[:name]`. To fix this, change the `find_by` to `where` – AlexQueue Oct 30 '14 at 21:01
  • 1
    It is absolutely simular output from index and show action. Even index action with params[:name] works in the same way as without. – p0ison Oct 31 '14 at 11:19
  • Maybe you could have a look here: https://stackoverflow.com/questions/12485404/how-to-implement-multiple-different-serializers-for-same-model-using-activemodel – schmijos Jul 04 '19 at 06:02

0 Answers0