2014-01-30 59 views
1

我正在使用Instagram API來在Ruby on Rails中構建網站。 我似乎無法弄清楚如何將任何圖像的標題文字拉出來,而且似乎沒有說明如何在文檔的任何位置執行此操作。是否有可能使用API​​或我必須使用別的東西?如何使用Instagram API來拉動Instagram圖片標題文字?

我已經附加2個非常簡單的代碼片段來說明以下

紅寶石我的問題:

class HomeController < ApplicationController 
    def index 
    def index 
     @instagram = Instagram.tag_recent_media("blablabla", {:count => 50}) 
    end 
    end 
end 

下面是HTML:

<div class = "tagged-images-container-position"> 
    <% @instagram.each do |instagram| %> 
      <%= instagram.caption.text %> <!-- THIS LINE DOESN'T WORK --> 
      <%= instagram.user.username %> 
      <%= image_tag(instagram.images.standard_resolution.url, class: "tagged-images") %> 
    <% end %> 
    </div> 
+1

是否有錯誤消息?那是ruby instagram庫嗎?也許你可以在這裏發帖:https://github.com/Instagram/instagram-ruby-gem。我使用C#並手動分析響應,並可以獲得標題文本沒有問題。我確實有標題爲空的情況,因此獲取.text是不可能的。所以我只是首先檢查空) –

+0

這實際上是問題。謝謝Sebastien!我只需要檢查標題是否先存在。 – shinjin

+0

我把它當作答案! Groovy的! –

回答

1

嘗試檢查空字幕文本在去文本propoerty之前。我發現有時它會彈出爲空。

1

具體來說,檢查整個標題,看它是否是零,挖下來caption.text

該技術已爲我工作之前:

if instagram.caption 
    caption = instagram.caption.text 
else 
    caption = "Oops, no caption." 
end