简体   繁体   English

Rails,Grape从集合中创建自定义JSON

[英]Rails, Grape create custom JSON from collection

I started to learn how to use Grape. 我开始学习如何使用葡萄。 I have collection with a lot of attributes and want only some of them. 我有很多属性的集合,只想要其中一些。 I did something like this: 我做了这样的事情:

get :all_elements do
    [
      my_collection.each do |element|
          {
           id: element.id,
           name: element.name
          }
      end
    ]
end

However this is not working. 但是,这不起作用。 How can I create custom json array from collection? 如何从集合创建自定义json数组?

Please try this code. 请尝试此代码。

list = my_collection.map do |element|
           { :id => element.id,
             :name => element.email
           }
       end

list.to_json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM