RoR:输出散列,获得正确的参数



我的输出是这样的:

#<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

我不明白?

谢谢

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

我用

解决了这个问题:
<% unless media.caption.blank? %>
  <%= media.caption.text %>
<% end %>

我假设media是这个对象。标题是什么?我没有在你的对象中看到任何这样的参数,那么你为什么认为它存在呢?很明显,它抛出未定义方法'[…]因为你的属性不存在。

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

我从您提供的链接中复制了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."

最新更新