是否有可能将jSON对象转换为ruby / rails中的美丽视图html.erb?

时间:2012-08-11 04:08:18

标签: ruby-on-rails json view

想象一下,我有这个:

[ 
  {"id":1, "name":"bla", "other":"aaaa"},
  {"id":2, "name":"ble", "other":"eeee"},
  {"id":3, "name":"bli", "other":"iiii"},
]

我想在someview.html.erb

中看到这一点
Id:1
name:Bla
other:aaaa

Id:2
name:Ble
other:eee

...

当我说JSON时,我的意思是我不知道对象属性。我只是想自动格式化JSON。

1 个答案:

答案 0 :(得分:1)

试试这个:

require 'JSON'
require 'yaml'

x = '[
  {"id":1, "name":"bla", "other":"aaaa"},
  {"id":2, "name":"ble", "other":"eeee"},
  {"id":3, "name":"bli", "other":"iiii"}
]'

puts JSON.parse(x).to_yaml