Ruby on Rails Advanced JSON Serialization

前端 未结 7 938
闹比i
闹比i 2020-12-13 02:53

I\'m looking to render an index of all articles along with a full article via JSON in my rails app, but I\'m having a little trouble figuring out how to do it.

Here

7条回答
  •  生来不讨喜
    2020-12-13 03:22

    Thanks for the question, I am able to customize my json format for a model with several associations.

    render json: @posts.to_json(

    :except => [ :created_at, :updated_at, :user_id ],

    :include => {

    :user => {:only => [:email, :phone]},

    :location => {:only => [:title, :lat, :lon, :street, :city, :state, :zipcode]},

    :uploads => {:only => [:image]}

    }
    )

提交回复
热议问题