-1

我有这样的输出:

#<Hashie::Mash created_time="1366008641" 
from=#<Hashie::Mash full_name="Cor Valen" id="22340" username="_corin"> 
id="4344344286" text="Look Who It Is, My Brother, My Favorite Deputy">

如果我这样做,这是一个输出:

<%= media.caption %>

我想得到这个text部分,我这样做了:

<%= media.caption.text %> 
gets me error: undefined method `text' for nil:NilClass

<%= media.caption[:text] %>
gets me error: undefined method `[]' for nil:NilClass

我不明白?

谢谢

4

3 回答 3

1

我在 Instagram 提要中遇到了这个问题,我发现某些媒体项目没有标题,所以抛出了错误。

我使用以下方法解决了这个问题:

<% unless media.caption.blank? %>
  <%= media.caption.text %>
<% end %>
于 2013-10-27T21:50:23.413 回答
0

我从您提供的链接中复制了 JSON 并运行它irb,请查看输出:

irb(main):004:0> media = Hashie::Mash.new(JSON.parse(File.open("inst.json").read))
=> #<Hashie::Mash data=[#<Hashie::Mash caption=#<Hashie::Mash created_time="1296656006" from=#<Hashie::Mash full_name="" id="1127272" type="user" username="cocomiin"> id="26329105" text="This is dummy text."> comments=#<Hashie::Mash> created_time="1296655883" filter="Gotham" id="22518783" images=#<Hashie::Mash low_resolution=#<Hashie::Mash height=306 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg" width=306> standard_resolution=#<Hashie::Mash height=612 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg" width=612> thumbnail=#<Hashie::Mash height=150 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg" width=150>> likes=#<Hashie::Mash count=35 data=[#<Hashie::Mash full_name="Kevin S" id="4" profile_picture="" username="mikeyk">, #<Hashie::Mash>]> link="http://instagr.am/p/BV5v_/" location=nil tags=[] type="image" user=#<Hashie::Mash full_name="Cocomiin" id="1127272" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg" username="cocomiin">>]>
irb(main):005:0> media = media.data[0]
=> #<Hashie::Mash caption=#<Hashie::Mash created_time="1296656006" from=#<Hashie::Mash full_name="" id="1127272" type="user" username="cocomiin"> id="26329105" text="This is dummy text."> comments=#<Hashie::Mash> created_time="1296655883" filter="Gotham" id="22518783" images=#<Hashie::Mash low_resolution=#<Hashie::Mash height=306 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg" width=306> standard_resolution=#<Hashie::Mash height=612 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg" width=612> thumbnail=#<Hashie::Mash height=150 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg" width=150>> likes=#<Hashie::Mash count=35 data=[#<Hashie::Mash full_name="Kevin S" id="4" profile_picture="" username="mikeyk">, #<Hashie::Mash>]> link="http://instagr.am/p/BV5v_/" location=nil tags=[] type="image" user=#<Hashie::Mash full_name="Cocomiin" id="1127272" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg" username="cocomiin">>
irb(main):006:0> media.caption.text
=> "This is dummy text."
于 2013-04-16T06:37:28.467 回答
0

我假设媒体就是这个对象。字幕是什么?我在你的对象中没有看到任何这样的参数,那么你为什么认为它存在呢?很明显,它会为 nil:NilClass 抛出未定义的方法“[...]”,因为您的属性不存在。

您可能正在寻找media.from.text,而不是media.caption.text

于 2013-04-15T07:30:48.687 回答