I'm trying to build a table of JSON data in a custom page in Active Admin. The JSON response is fairly deeply nested so I'm using a lot of loops. Here's as far as I've been able to get/some of what I've tried:
panel "Boxes" do
boxes.each do |box| #isolate each hash item in the array
# table_for box.values do
box.each do |key, value|
if value.is_a?(Hash) #if value of a hash is a hash
columns do
value.each do |k, v| #iterate through the hash
text_node "#{k}: #{v}"
end
end
elsif value.is_a?(Array) #there's only one value that's an array & the key is "Products"
columns do
value.each do |products_array|
columns do
products_array.each do |k, v|
if v.is_a?(Hash)
v.each do |kk, vv|
if vv.is_a?(Hash)
vv.each do |kkk, vvv|
text_node "#{kkk}: #{vvv}, "
end
else
text_node "#{kk}: #{vv}, "
end
end
else
text_node "#{k}: #{v}, "
end
end
end
end
end
else
# key.each do
# column key
# end
end
end
# end
end
I'm looking for general guidelines as to how to make a table in a custom Active Admin page as well as how to access/display deeply nested array/hash attributes. Thanks!
Try a recursive helper to convert the JSON to the string you want:
def nested_hash_to_s(h)
h.map { |k, v| v.is_a?(Hash) nested_hash_to_s(v) : "#{k}: #{v}" }.join(", ")
end
then your column simplifies to:
columns do
text_node nested_hash_to_s(products_array)
end
There might be other issues, but that's a start.
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.