2012-02-17 54 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