Noobie here.
The gist of my problem is this. If I move from serializer file from
app/serializers/ to app/models/ then everything works fine. My JSON API
will output my data according to my serializer. However, if I remove the
copy from the app/models/ directory… it no longer works and goes back
to Rails default JSON output.
What do I need to do to include my serializer file in app/serializers/?
/app/serializers/item_serializer.rb
class ItemSerializer < ActiveModel::Serializer
attributes :id, :name
end
/app/controllers/items_controller.rb
class ItemsController < ApplicationController
respond_to :json
def index
items = Item.all
respond_with ({ success: :true, items: items }.as_json)
end
def show
@item = Item.find_by_id(params[:id])
if @item
render json: @item, serializer: ItemSerializer
else
render json: { success: :false, error: "Could not find
item."}, status: :not_found
end
end
end
Do I HAVE TO have my serializers in /app/models by default. Why would
ActiveModel Serializers default generating a new serializer then to the
/app/serializers directory. How do I let my controller know about my
serializer?