How to pull Instagram image caption text using the Instagram API?

瘦欲@ 提交于 2021-02-11 06:17:52

问题


I'm using the Instagram api to build a site in Ruby on Rails. I can't seem to figure out how to pull caption text with any of the images, and it doesn't seem to state how to do this anywhere in the documentation. Is it possible using the api or do I have to use something else?

I've attached 2 very simple code snippets to illustrate my problem below

Ruby:

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

Here is the 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:


Try checking the caption text for null before going for that text propoerty. I found that sometimes it pops up as null.




回答2:


Specifically, check the entire caption to see if it is nil, before digging down to caption.text

This technique has been working for me:

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


来源:https://stackoverflow.com/questions/21445805/how-to-pull-instagram-image-caption-text-using-the-instagram-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!