include_root_in_json from controller

后端 未结 2 1818
一生所求
一生所求 2020-12-19 03:15

I\'m trying to return a result to a view where the json does not include the root.

I don\'t want to set this on all actions for this model,so therefore am trying to

2条回答
  •  感动是毒
    2020-12-19 03:26

    It's an old question now, but the answers were not updated, and Rails 3.2 filled the gap (Rails source)

    With Rails 3.2, you can now use:

    my_model.to_json(:root => false)
    

    if you don't want the root key to be included.

    It gets even better as I just tested it with an array, so you can do:

    Model.all.to_json(:root => true)
    

提交回复
热议问题