2

我使用這種寶石:https://github.com/kylejginavan/youtube_it問題 - 的Rails 3.1

VideosController看起來是這樣的:

class VideosController < ApplicationController 
    def index 
    client = YouTubeIt::Client.new(:username => YouTubeITConfig.username , :password => YouTubeITConfig.password , :dev_key => YouTubeITConfig.dev_key) 
@video = client.videos_by(:user => 'marcamillion1983') 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @video } 
    end 
    end 

我的觀點:

<%= @video %> 

輸出是這樣的:

#<YouTubeIt::Response::VideoSearch:0x0000012995f7c0> 

當我試圖做@video.each do |video|它給我一個未知的方法each錯誤。

然而,當我嘗試在Ruby提示,查詢,它返回的結果:

ruby-1.9.2-p0 > client.videos_by(:user => 'marcamillion1983') 
=> #<YouTubeIt::Response::VideoSearch:0x000001290484c0 @feed_id="tag:youtube.com,2008:user:marcamillion1983:uploads", @updated_at=2011-09-14 10:32:31 UTC, @total_result_count=1, @offset=1, @max_result_count=25, @videos=[#<YouTubeIt::Model::Video:0x00000129049050 @video_id="tag:youtube.com,2008:video:VY-Ch8OpjvY", @published_at=2011-03-04 18:49:56 UTC, @updated_at=2011-04-02 20:13:14 UTC, @categories=[#<YouTubeIt::Model::Category:0x00000100b430b0 @term="Comedy", @label="Comedy">], @keywords=["laughing baby", "funny baby", "funny laugh", "hysterical laugh", "laughing hysterically", "baby", "funny video", "home video", "cute baby", "cute baby laugh", "5 month old"], @title="Baby Laughing Hysterically at Mummy", @html_content=nil, @author=#<YouTubeIt::Model::Author:0x00000100a308f8 @name="marcamillion1983", @uri="http://gdata.youtube.com/feeds/api/users/marcamillion1983">, @description="Mia-Kalani (5 months at the time) was about to eat and Mummy was playing with her when she started laughing, so she decided to record it.\n\nEnjoy :)", @duration=38, @media_content=[#<YouTubeIt::Model::Content:0x000001008424d8 @url="http://www.youtube.com/v/VY-Ch8OpjvY?f=user_uploads&d=ATs-A8zzS5exgl44oxOFQt4O88HsQjpE1a8d1GxQnGDm&app=youtube_gdata", @format=#<YouTubeIt::Model::Video::Format:0x00000101428220 @format_code=5, @name=:swf>, @duration=38, @mime_type="application/x-shockwave-flash", @default=true>, #<YouTubeIt::Model::Content:0x00000100840c28 @url="rtsp://v8.cache6.c.youtube.com/CksLENy73wIaQgn2jqnDh4KPVRMYDSANFEgGUgx1c2VyX3VwbG9hZHNyIQE7PgPM80uXsYJeOKMThULeDvPB7EI6RNWvHdRsUJxg5gw=/0/0/0/video.3gp", @format=#<YouTubeIt::Model::Video::Format:0x00000101428248 @format_code=1, @name=:rtsp>, @duration=38, @mime_type="video/3gpp", @default=false>, #<YouTubeIt::Model::Content:0x0000010083efe0 @url="rtsp://v5.cache4.c.youtube.com/CksLENy73wIaQgn2jqnDh4KPVRMYESARFEgGUgx1c2VyX3VwbG9hZHNyIQE7PgPM80uXsYJeOKMThULeDvPB7EI6RNWvHdRsUJxg5gw=/0/0/0/video.3gp", @format=#<YouTubeIt::Model::Video::Format:0x000001014281f8 @format_code=6, @name=:three_gpp>, @duration=38, @mime_type="video/3gpp", @default=false>], @player_url="http://www.youtube.com/watch?v=VY-Ch8OpjvY&feature=youtube_gdata_player", @thumbnails=[#<YouTubeIt::Model::Thumbnail:0x000001277e15d0 @url="http://i.ytimg.com/vi/VY-Ch8OpjvY/default.jpg", @height=90, @width=120, @time="00:00:19">, #<YouTubeIt::Model::Thumbnail:0x000001277e0fe0 @url="http://i.ytimg.com/vi/VY-Ch8OpjvY/hqdefault.jpg", @height=360, @width=480, @time=nil>, #<YouTubeIt::Model::Thumbnail:0x000001277e09f0 @url="http://i.ytimg.com/vi/VY-Ch8OpjvY/1.jpg", @height=90, @width=120, @time="00:00:09.500">, #<YouTubeIt::Model::Thumbnail:0x000001277e0400 @url="http://i.ytimg.com/vi/VY-Ch8OpjvY/2.jpg", @height=90, @width=120, @time="00:00:19">, #<YouTubeIt::Model::Thumbnail:0x000001277dfe10 @url="http://i.ytimg.com/vi/VY-Ch8OpjvY/3.jpg", @height=90, @width=120, @time="00:00:28.500">], @rating=#<YouTubeIt::Model::Rating:0x00000129001520 @min=1, @max=5, @rater_count=2, @average=5.0, @likes=2, @dislikes=0>, @view_count=276, @favorite_count=1, @widescreen=nil, @noembed=false, @racy=false, @where=nil, @position=nil, @latitude=nil, @longitude=nil, @state={:name=>"published"}>]> 

我如何得到正確的輸出,這樣我可以在我的視圖中顯示的視頻?

謝謝。

回答

2

雖然我沒有使用特定的寶石自己,它看起來本身存儲在返回的YouTubeIt::Response::VideoSearch對象的videos屬性的視頻。嘗試使用@video.videos.each do |video|,看看是否有效。

+0

完美...這工作。謝謝! – marcamillion

+0

一個簡單的問題,現在它返回一個瘋狂的散列,看起來像在上面的命令行中返回的值。我現在如何獲得散列內的每個屬性?例如說網址?我試過'@video.videos.each do | video |視頻。@網址「,但沒有奏效。 – marcamillion

+0

好的......我在'do'塊中嘗試了'video.categories',這似乎奏效了。將繼續探索,看看我能想出什麼。謝謝! – marcamillion

0

嘿我的朋友,我舉了一個例子來說明如何使用rails整合gem youtube_it。您可以測試這個在線或獲取代碼見

看到這裏http://youtube-it.heroku.com/

享受!