2012-02-17 55 views
0

我試圖去通過對dribbble第5杆,以Jeremys真棒dribbble庫(https://github.com/jeremyw/swish),得到他們的標題,然後在視圖中打印這些項目,但我不斷收到錯誤:#爲未定義的方法`title'。每個語句和運球API是給我找麻煩

@dribbbletimes = [] 
(1..5).each do |i| 
    @dribbbletimes << Dribbble::Shot.find(i).title 
end 

如果我用數字替換i,那麼它可以找到沒有問題的標題並打印5次。任何猜測爲什麼會發生這種情況?

回答

2

我剛剛試過你的代碼,看起來find方法在找不到任何東西時會返回一種不同類型的對象,所以當鏡頭號碼無效時調用title方法不起作用。

例如:

1.9.2-p290 :017 > Dribbble::Shot.find(2) 
=> #<Dribbble::Shot:0x007ff96b3ed110 @player=#<Dribbble::Player:0x007ff96b3ed048 @created_at="2009/07/21 19:32:24 -0400", @shots_count=172, @twitter_screen_name="owltastic", @avatar_url="http://dribbble.s3.amazonaws.com/users/4/avatars/original/Meagan-6.jpg?1309446649", @likes_received_count=14937, @name="Meagan Fisher", @location="Brooklyn, NY", @following_count=445, @likes_count=1080, @website_url="http://owltastic.com", @username="owltastic", @url="http://dribbble.com/owltastic", @rebounds_count=3, @draftees_count=44, @id=4, @drafted_by_player_id=1, @followers_count=9979, @comments_received_count=1795, @comments_count=211, @rebounds_received_count=13>, @created_at="2009/07/23 07:07:53 -0400", @short_url="http://drbl.in/c", @image_url="http://dribbble.com/system/users/4/screenshots/2/Picture-2.png?1309017100", @title="Playing with Dribbble", @likes_count=9, @rebound_source_id=nil, @url="http://dribbble.com/shots/2-Playing-with-Dribbble", @rebounds_count=0, @id=2, @image_teaser_url="http://dribbble.com/system/users/4/screenshots/2/Picture-2_teaser.png?1309017100", @height=300, @views_count=13299, @comments_count=2, @width=400> 

1.9.2-p290 :018 > Dribbble::Shot.find(3) 
=> #<Dribbble::Shot:0x007ff96b3ced50 @created_at=nil, @message="Not found"> 

返回的對象是將它添加到陣列之前確實有效,您應該區分開來。

更新
周圍一點點挖後,我發現一個無效的拍攝返回的created_at屬性的零值,所以你可能會做這樣的事情:

@dribbbletimes = [] 
(1..5).each do |i| 
    shot = Dribbble::Shot.find(i) 
    @dribbbletimes << shot.title unless shot.created_at.blank? 
end 
+1

你在開玩笑我。我覺得很愚蠢。如果沒有StackOverflow上的人,我該怎麼做。 – 2012-02-17 22:23:04

+0

上檢查/跳躍拍​​攝,如果沒有標題有什麼建議? – 2012-02-17 22:24:01

+0

我只是增加了一個=) – bruno077 2012-02-17 22:24:20