Rails Select from JSON Array

后端 未结 1 1645
感动是毒
感动是毒 2021-01-07 12:36

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 p

相关标签:
1条回答
  • 2021-01-07 12:50

    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.

    0 讨论(0)
提交回复
热议问题