Reputation: 87
I am trying to display a given value from a block of search data per result in the listing. I am getting the error "can't convert String into Integer" How do I display the next level array inside my linked_agents?
My code for the particular column is such
<td>
<% if result['primary_type'] === "resource" or result['primary_type'] === "digital_object" or result['primary_type'] === "accession" %>
<%= display_agents(result) %>
<% end %>
</td>
and the code
def display_agents(hash, opts = {})
object = JSON.parse( hash["json"] )["linked_agents"]
object2 = object["_resolved"]
html = "<div class='audit-display-compact'><small>"
html << "<ul style='list-style-type:none'>"
html << "<li>#{object2}</li>"
html << "</ul>"
html << "</small></div><div class='clearfix'></div>"
html.html_safe
end
Here is what is inside linked_agents.
[{"_resolved"=>{"names"=>[{"sort_name"=>"John Smith"}]}}]
How can I get all the sort_name's to display the data? There could be more than one _resolved each containing a sort_name.
Thanks
Upvotes: 1
Views: 467
Reputation: 1645
More data would've been more helpful than less data, but essentially your problem is this: "json" contains a string. Nothing more. Not an object. Nothing you could reference with results[]
. Assuming the JSON is all properly formatted, what you would need to do is something like this:
<%= JSON.parse( result["json"] )["linked_agents"]["_resolved"]["sort_name"] %>
Now, this is horribly sloppy, and not the way you're going to want to accomplish your goals (assuming this is even effective with your data set). In actuality, what you'll want to do is parse that JSON into a variable, and work through in hash form.
It is difficult to clearly keep the topics at work here because there isn't a clear question. I would encourage you to make sure you have a basic familiarity of ruby arrays, hashes, and nested structures consisting thereof. Additionally, take a look at how you're retrieving the data that goes into results["json"]
.
There are many ways to accomplish many things once your data is properly structured, but you need to have a clear understanding of these types of structure before you can work magic (and you need to possess a basic knowledge of what you aren't understanding before we can provide you with solutions).
I hope that is helpful. Feel free to discuss points of confusion in the comment, and I will update this answer as we approach a solution for your problem.
Upvotes: 1