【问题标题】:Rails - using jBuilder to create json fileRails - 使用 jBuilder 创建 json 文件
【发布时间】:2012-10-03 03:11:39
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: ruby-on-rails json jbuilder


    【解决方案1】:

    我不需要在视图文件中编码为 json。 您的视图文件应如下所示:

    json.id current_user.id
    json.name current_user.name
    

    【讨论】:

      猜你喜欢
      • 2012-10-03
      • 2012-12-14
      • 2015-11-14
      • 2013-05-13
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      相关资源
      最近更新 更多