问题
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