Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easiest way to serialized outside of a controller? #947

Closed
axsuul opened this issue Jun 10, 2015 · 4 comments
Closed

Easiest way to serialized outside of a controller? #947

axsuul opened this issue Jun 10, 2015 · 4 comments

Comments

@axsuul
Copy link

axsuul commented Jun 10, 2015

Currently, it seems like the best way to serialize outside a controller with the JSON adapter is

serializer = PostSerializer.new(post)
adapter = ActiveModel::Serializer::Adapter.create(serializer)

# output
adapter.to_json

Is this is the most direct way to do it? Or perhaps any recommendations?

@axsuul axsuul changed the title Easiest way to render outside of a controller? Easiest way to serialized outside of a controller? Jun 10, 2015
@bf4
Copy link
Member

bf4 commented Jun 11, 2015

that's close enough.. see how I wrote some tests here #878 (comment) (but why are you serializing outside of the controller). the tl;dr of how AMS does it is https://github.com/rails-api/active_model_serializers/blob/master/lib/action_controller/serialization.rb#L38-L48

@joaomdmoura
Copy link
Member

@axsuul instead .to_json use . as_json.
But as @bf4 said, why are you trying to serialize it outside the controller?
If possible, could you try to create this question at Stackoverflow and send over the link? We are trying to focus the questions there 😄 I'm closing it for now

@axsuul
Copy link
Author

axsuul commented Jun 13, 2015

I'm trying to send a json object to a webhook, using the same serializer that I return as an API response. Hope that makes sense, and thanks for the responses!

@posgarou
Copy link

posgarou commented Jul 3, 2015

Manual serializing is also useful if you're using the react_component helper that ships with React-Rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants