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
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