Skip to content

Serializer files were broken by versioning #1597

Closed
@hightech

Description

@hightech

I added the api versioning feature in my project. My serializer files were broken by versioning.

For example, when I didn't add those lines in ./app/serializers/api/v1/user_serializer.rb file but I added in only controllers files.

module Api
  module V1
   .....
  end
end

It performed correct json file output the way I want it. But if I decided to add versioning in serializer files, the json file output doesn't show the same results like the output (without versioning feature on serializer files). Any idea why it doesn't perform correct like "without version feature" in serializer files. Is this a bug or not? Any suggestion appreciated. Thanks.

Here is my snippet code:

Serializers

./app/serializers/api/v1/user_serializer.rb

module Api
   module V1
    class UserSerializer < ActiveModel::Serializer
            attributes :id, :username
            has_many :entries
    end
  end
end
./app/serializers/api/v1/entry_serializer.rb

module Api
   module V1
    class EntrySerializer < ActiveModel::Serializer
            attributes :id, :description, :created_at, :updated_at

     end
    end
end

Routes

./config/routes.rb

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :entries, except: [:new, :edit]
      resources :users, except: [:new, :edit]
    end
  end
end

Controllers

./app/controllers/api/v1/users_controller.rb

module Api
  module V1
    class EntriesController < ApplicationController

      before_action :set_entry, only: [:show, :update, :destroy]

      def index
        @entries = Entry.all
        render json: @entries
      end
    .
    .
    .
    end
  end
end

Models

./app/models/user.rb

class User < ActiveRecord::Base
    has_many :entries
end
./app/models/entry.rb

class Entry < ActiveRecord::Base
    belongs_to :user
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions