jsonapi-rb / jsonapi-rails

Rails gem for fast jsonapi-compliant APIs.
http://jsonapi-rb.org
MIT License
318 stars 62 forks source link

to_json doesnt work #102

Closed brentgreeff closed 5 years ago

brentgreeff commented 5 years ago

I want to pass JSON to a react component.

= react_component('ActivityList', props: {activities: @campaign.activities.to_json}, prerender: false)

to_json ignores the SerializableActivity

Things work fine if rendering from the controller with render jsonapi: @campaign.activities

How can I get the right JSON in the view?

beauby commented 5 years ago

Yes, in the render jsonapi: call, your @campaign.activities get wrapped within instances of SerializableActivity, which does not happen when you call to_json directly on your ActiveRecords. You're probably looking for something along the lines of:

renderer = JSONAPI::Serializable::Renderer.new

activities = renderer.render(@campaign.activities)