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 ofActiveModel
, so you should be able to simply callto_json
on it with all of the arguments that that supports. You should also be able to defineas_json
andfrom_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