[英]Rails - using jBuilder to create json file
我正在尝试使用jBuilder创建一个json文件(根据timeline.verite.co时间线要求)-但结果只是空白,而不是一对{}。 该文件的html版本正在获取正确的信息,所以不是因为当前用户没有任何事件。 有人知道为什么吗? 谢谢!
这是文件( @events
指当前用户的所有事件):
json.key_format! :camelize => :lower
Jbuilder.encode do |json|
json.timeline do
json.headline current_user.first_name
json.type "default"
json.text "A Timeline"
json.start_date
json.array!(@events) do |event|
json.start_date event.start_date
json.end_date event.end_date
json.headline event.headline
json.text event.text
json.asset do
json.media event.media
json.credit event.credit
json.caption event.caption
end
end
end
end
更新:
我已经尝试了一些非常简单的方法,但是文件仍然空白。 我不知道为什么!!
这仍然给出一个空白文件:
Jbuilder.encode do |json|
json.id current_user.id
end
我不需要在视图文件中编码为json。 您的视图文件应如下所示:
json.id current_user.id
json.name current_user.name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.