Rails从JSON数组中选择 [英] Rails Select from JSON Array

查看:112
本文介绍了Rails从JSON数组中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从Foursquare返回的JSON数组;我们称之为@venues.我希望能够通过下拉框选择"场所,并且希望它成为表单的一部分.

I have a JSON array that was returned from Foursquare; let's call it @venues. I want to be able to "select" a venue via a drop down box and I want it to be part of a form.

这意味着我希望能够通过名称选择一个特定的场地(在本例中为Hotel Utah Saloon),并将其保存在模型中.为了澄清起见,我将仅保存我选择的场所,而不是所有场所.

That means I want to be able to select a specific venue by name (i.e. in this case, Hotel Utah Saloon), and save it's id into a model. To clarify, I will only be saving the venues I select, not all of them.

通过研究,我发现自己对selectcollection_selectselect_tag感到困惑.请记住,这是直接来自JSON.parse方法的JSON,而不是数据库模型.

Through research, I have found myself confused between select, collection_select, and select_tag. Keep in mind that this is JSON directly from a JSON.parse method and is not a DB model.

如何创建此下拉菜单?

为澄清起见,@venues看起来像这样:

For clarification, @venues looks something like this:

[
            {
               "beenHere":8,
               "venue":{
                  "id":"3fd66200f964a52023f11ee3",
                  "name":"Hotel Utah Saloon",
                  "contact":{
                     "phone":"4155466300",
                     "formattedPhone":"(415) 546-6300",
                     "twitter":"hotelutah"
                  },
                  "location":{
                     "address":"500 4th St",
                     "crossStreet":"Corner of Bryant",
                     "lat":37.77947007181946,
                     "lng":-122.39816943501836,
                     "postalCode":"94107",
                     "city":"San Francisco",
                     "state":"CA",
                     "country":"United States",
                     "cc":"US"
                  },
                  "categories":[
                     {
                        "id":"4bf58dd8d48988d1e9931735",
                        "name":"Rock Club",
                        "pluralName":"Rock Clubs",
                        "shortName":"Rock Club",
                        "icon":"https:\/\/foursquare.com\/img\/categories\/arts_entertainment\/musicvenue_rockclub.png",
                        "parents":[
                           "Arts & Entertainment",
                           "Music Venues"
                        ],
                        "primary":true
                     }
                  ],
                  "verified":true,
                  "stats":{
                     "checkinsCount":6654,
                     "usersCount":3330,
                     "tipCount":50
                  },
                  "likes":{
                     "count":0,
                     "groups":[

                     ]
                  },
                  "beenHere":{
                     "count":0
                  }
               }
            }
        ]

推荐答案

控制器

@venues = JSON.parse @venues

查看

<%= select(:model, :venue_id, @venues.map {|v| [ v['venue']['name'], v['venue']['id'] ] }) %>

或更干净一点:

控制器

@venues = JSON.parse @venues
@venues_list = @venues.map { |v| v['venue'] }

查看

<%= select(:model, :venue_id, @venues_list.map {|v| [ v['name'], v['id'] ] }) %>

有关选择助手的详细信息.

这篇关于Rails从JSON数组中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆