繁体   English   中英

Rails-使用JBuilder构建自定义JSON

[英]Rails - Building custom JSON using JBuilder

我目前正在开发一个个人Rails 4项目,并且在一个控制器中,我有一个由三个不同对象组成的数组,我正尝试将其转换为JSON。 我正在使用find_by_sql方法通过自定义MySQL查询来查询数据库。 代码如下:

控制器:

class ActivitiesController < ApplicationController
  def index
    user = current_user.id # using devise
    limit = 100

    likes = Like.find_by_sql(...)
    shares = Share.find_by_sql(...)
    comments = Comment.find_by_sql(...)

    @activities = [likes, shares, comments]
  end
end

三个对象中的每一个都包含不同的字段。 例如, likes对象包含字段,诸如POST_ID, 名字姓氏shares对象包含字段,诸如POST_ID,SHARE_COUNT,shared_time

这是我要输出的JSON:

{
    "likes": [
      { "post_id": "7", "first_name": "Yss", "last_name": "Salas", "profile_pic": "345943754_o.png" },
      { "post_id": "34", "first_name": "Jessica", "last_name": "Nigri", "profile_pic": "pikachu.png" }
    ],
    "shares": [
      { "post_id": "43", "share_count": 54, "shared_time": "2014-05-04 15:14:45" },
      { "post_id": "54", "share_count": 17, "shared_time": "2014-05-24 03:43:45" }
    ],
    "comments": [
      { "post_id": "34", "first_name": "Yss", "last_name": "Salas", "comment": "¡Me gustas mucho!" },
      { "post_id": "12", "first_name": "Jenna", "last_name": "Marbles", "comment": "Blah blah blah look at my ugly dog awwwww!" }
    ]
}

index.json.jbuilder:

json.array! @activities do |subactivities|
  json.array! subactivities do |activity|
    if activity.class == Like
      json.likes.post_id activity.post_id #exception raised in this line
      json.likes.title activity.title
      json.likes.first_name activity.first_name
      json.likes.last_name activity.last_name
      json.likes.profile_pic activity.profile_pic
    elsif activity.class == Share
      json.shares.post_id activity.post_id
      json.shares.share_count activity.share_count
      json.shares.shared_time activity.shared_time
    else
      json.comments.post_id activity.post_id
      json.comments.first_name activity.first_name
      json.comments.last_name activity.last_name
      json.comments.comment activity.comment
    end
  end
end

上面的NoMethodError in Activities#index, undefined method 'post_id' for #<Object:0x00000003064250>引发了NoMethodError in Activities#index, undefined method 'post_id' for #<Object:0x00000003064250>异常NoMethodError in Activities#index, undefined method 'post_id' for #<Object:0x00000003064250> 我是Rails的新手,我猜测这是因为没有json.likes属性来将值分配给它而发生的。 如何使用JBuilder构建上述JSON结构?

您可以在没有JBuilder的情况下轻松,快速地做到这一点。

def index
  user = current_user.id # using devise
  limit = 100

  likes = Like.find_by_sql(...).map(&:attributes) # this will return an array of JSON objects
  shares = Share.find_by_sql(...).map(&:attributes)
  comments = Comment.find_by_sql(...).map(&:attributes)

  @activities = {"likes" => likes, "shares" => shares, "comments" => comments}
end

这样,您便拥有了所需的所有信息的哈希。 将查找喜欢,共享和评论的逻辑提取到一个单独的类中可能也更好,这样您就可以调用InformationCollector.collect并将为您提供所需的响应。

在Jbuilder的GitHub页面上 ,解决方案非常简单。 所有需要做的就是将实例变量传递到一个块中,然后手动分配自定义键/值对。

下面的示例将为@likes实例变量生成键/值对的数组:

# activities/index.json.jbuilder

json.likes @likes do |like|
  json.post_id like.post_id
  json.first_name like.user.first_name
  json.last_name like.user.last_name
  json.profile_pic like.user.profile_pic
end

以上将输出:

{
  "likes": [
    { "post_id": 7, "first_name": "Yss", "last_name": "Salas", "profile_pic": "345943754_o.png" },
    { "post_id": 34, "first_name": "Jessica", "last_name": "Nigri", "profile_pic": "pikachu.png" }
  ],
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM