Closed patrickgordon closed 8 years ago
Looks like it's related to using different module nesting, which affects Ruby's constant lookup scope.
module API::V1
and
module API
module V1
and then possibly how Rails autoloading of undefined constants works
have you tried using the absolute namespace? each_serializer: ::API::V1::ContactsSerializer
?
This:
class ContactsSerializer < ActiveModelSerializers
should be
class ContactsSerializer < ActiveModel::Serializer
I chose to go with a different gem. Thanks anyway for your responses.
@patrickgordon Thanks, would you mind sharing what you're using?
@bf4 I ended up going with this gem https://github.com/fotinakis/jsonapi-serializers
Cheers!
I'd recommend https://github.com/cerebris/jsonapi-resources if it's not too late
B mobile phone
On Apr 25, 2016, at 5:30 PM, Patrick notifications@github.com wrote:
@bf4 I ended up going with this gem https://github.com/fotinakis/jsonapi-serializers
Cheers!
— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub
For anyone looking to achieve this in AMS, @bf4 suggestion works fine, to be explicit, the code in the serializer in api/v1/contacts_serializer.rb
should be changed to
module Api::V1
class ContactsSerializer < ActiveModelSerializers
attributes :first_name, :last_name, :email, :coffee_preference, :readable_date_of_birth, :age, :favourite
...
end
end
Any help would be fantastic.
Expected behavior vs actual behavior
Expected:
render json: @contacts, each_serializer: ContactsSerializer
should work normallyActual: Get error message:
superclass must be a Class (Module given)
Steps to reproduce
(e.g., detailed walkthrough, runnable script, example application)
controller in folder:
app/controllers/api/v1/contacts_controller.rb
serializer in folder:app/serializers/api/v1/contacts_serializer.rb
Code in Controller:
Code in serializer:
Environment
ActiveModelSerializers Version 0.10:
Output of
ruby -e "puts RUBY_DESCRIPTION"
: ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin15]OS Type & Version: Mac OSX 10.11.3
Integrated application and version (e.g., Rails, Grape, etc): Rails 4.2.6
Backtrace
(e.g., provide any applicable backtraces from your application)