Ruby on Rails Advanced JSON Serialization

前端 未结 7 937
闹比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:33

    I would recommend overloading the attributes method to return an alternat hash that will be automatically used in to_json output.

    class Article
       def attributes
         { ... } # define your hash that you want to return at the '...'
       end
    end
    

    To me this seems much simpler than mucking around with to_json directly.

提交回复
热议问题