Rails Select from JSON Array

独自空忆成欢 提交于 2019-12-30 11:49:29

问题


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.

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.

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.

How do I create this drop down in a form?

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
                  }
               }
            }
        ]

回答1:


Controller

@venues = JSON.parse @venues

View

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

Or to be more cleaner:

Controller

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

View

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

More information about select helper.



来源:https://stackoverflow.com/questions/12595327/rails-select-from-json-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!