2012-06-28 24 views
0

我使用this gem來搜索youtube視頻並在列表框中顯示ajax。我想從哈希結果中獲得標題和video_id,但不知道如何去做。我試過下面的代碼,但它顯示undefined method title for #<Hash:0x0000000582b270>映射youtube_search列表框的散列結果

這是我的代碼,我想要[:title,:video_id]數組,因此它可以顯示在列表框中。謝謝。

search_result = YoutubeSearch.search(params[:query]) 
render :json => search_result.map{|k,v| [k.title, v.video_id] } 

回答

1

該搜索方法單獨地返回視頻數據散列數組,而不僅僅是一個。
而你從哈希值獲得值的方式是這樣的:

results = {} 
search_result.each do |s| 
    results[s['id']] = s['title'] 
end 

puts results