I have a model with json field and I want to prettify the output for this field. How can I do it?
show do
attributes_table do
row :source_json do |model|
model.source_json
end
end
end
Current field looks like this:
{"date"=>"2018-12-17", "value"=>"sample"}
I want something like this:
{
"date"=>"2018-12-17",
"value"=>"sample"
}
I would go with something like this:
show do
attributes_table do
row :source_json do |model|
JSON.pretty_generate(JSON.parse(model.source_json))
end
end
end
You might not need the JSON.parse
call if you have an option to get source
as a Ruby hash instead of a JSON string.
You might want to wrap the output into a <pre>
HTML tag – like Evan Ross suggested – to improve readability:
show do
attributes_table do
row :source_json do |model|
tag.pre JSON.pretty_generate(JSON.parse(model.source_json))
end
end
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.