vendredi 15 janvier 2016

Trying to display fields from has but getting undefined method `[]' for nil:NilClass

I am receiving the error "undefined method `[]' for nil:NilClass" when trying to display a field from a json parsed hash. How do I get the coll_title data to display?

The data from alinks:

[{
"role"=>"source", 
"relator"=>"dnr", 
"terms"=>[], 
"_resolved"=>{
    "lock_version"=>0, 
    "publish"=>true, 
    "display_name"=>{
        "coll_num"=>"024", 
        "coll_title"=>"Collection Title"
        }
    }
}]

The code:

def display_agents(hash, opts = {})
  html = "<div><small>"
  html << "<ul style='list-style-type:none'>"
  alinks = JSON.parse( hash["json"] )["linked_agents"]
  alinks.each do |_resolved|
    html << "<li>#{_resolved["display_name"]["coll_title"]}</li>"
  end
  html << "</ul>"
  html << "</small></div><div class='clearfix'></div>"
  html.html_safe
end

Aucun commentaire:

Enregistrer un commentaire