2

In Active node how we can implement JSON serializers like active modal

http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html

Or any other way to send customized API response?

Right now I am doing it by making custom method in controller in which response is made after iterations (no. of records)

posts.map{|x| serialize_post(x) }

def serialize_post (post)
  {
    id: post.id,
    name: post.name
  }
end
Vishal G
  • 1,521
  • 11
  • 30

1 Answers1

2

The ActiveNode gem is based off of ActiveModel, so you should be able to simply call to_json on it with all of the arguments that that supports. You should also be able to define as_json and from_json methods:

http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html

Separately, I'm also a big fan of the JSON API standard, so for that you might want to check out the jsonapi-serializers gem

Edit:

Active model serilaizer Gem also fulfill the purpose

https://github.com/rails-api/active_model_serializers

Vishal G
  • 1,521
  • 11
  • 30
Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • any way to include this in base controller jsonapi-serializers I am using ActionController::Metal or any other gem for it? – Vishal G Dec 23 '16 at 17:07
  • I'm not sure exactly, but this example seems to integration `jsonapi-serializers` with Rails: https://github.com/fotinakis/jsonapi-serializers#rails-example – Brian Underwood Dec 23 '16 at 20:18