简体   繁体   中英

Ruby on Rails what does this hash output mean?

#<Hashie::Mash created_time="1366084479" from=#<Hashie::Mash 
full_name="alyssabri_" id="24110592" username="ally"> id="4350706" 
text="Some Text">

What does this mean? I get it when I do this:

    <% (@arr).each do |media| %>
        <%= media.caption %>
    <% end %>

I'm trying to get the text which is in caption ?

Thanks

This is telling you that media.caption is an instance of Hashie::Mash (a gem you have installed in your application provides Hashie , similar to Ruby's native Hash ). You need to get the text key/value pair from that Hashie instance

<% (@arr).each do |media| %>
    <%= media.caption.text %>
<% end %>

To answer your question about how to gracefully bypass those media instances with no related caption , you can do something like this

<% (@arr).each do |media| %>
    <%= media.caption.text unless media.caption.blank? %>
<% end %>

or filter out those caption -less media objects up front

<% @arr.select{ |m| m.caption.present? }.each do |media| %>
    <%= media.caption.text %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM