1

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

1 に答える 1

1

コントローラ

@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'] ] }) %>

selecthelperに関する詳細情報。

于 2012-09-26T07:52:29.293 に答える