Rails 3 respond_with json 问题

发布于 2024-11-06 12:23:36 字数 736 浏览 1 评论 0原文

生成一些 json 时遇到问题。我正在尝试渲染一个 单个活动记录结果转为 json,如下所示:

@data = User.find(1)
respond_with(@data, :include => :status)

json 结果是:

{
  -user: {
    address: null
    email: "[email protected]"
    first_name: "Test"
    last_name: "Man"
    status_id: 1
    username: "testguy"
    status: { }
  }
}

那么问题是什么?问题是 :include=>:status 似乎 不带出这种关系。在我的用户模型中我有一个 属于:状态。我如何让它在单个结果集上工作?

当我这样做时:

@data = User.where("id = 1")
respond_with(@data, :include => :status)

关系以这种方式显示在 json 结果集中。但其 在我不想要的对象数组中。

有什么想法吗?

Having trouble with generating some json. I am trying to render an
single active record result to json like this:

@data = User.find(1)
respond_with(@data, :include => :status)

The json result is:

{
  -user: {
    address: null
    email: "[email protected]"
    first_name: "Test"
    last_name: "Man"
    status_id: 1
    username: "testguy"
    status: { }
  }
}

So whats the problem? The problem is that the :include=>:status seems
to not bring over the relation. In my User model I have a
belongs_to :status. How do i get this to work on a single result set?

When I do this:

@data = User.where("id = 1")
respond_with(@data, :include => :status)

The relation shows in the json result set fine this way. But its
within an array of objects, which i do not want.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

过期情话 2024-11-13 12:23:36

我假设您想要包含的 状态 不是 http 状态代码之一(200、201 等),也不是以任何方式与 User 对象关联的对象,而是您自己的变量。

使用 Rails 3.0.10 和 Ruby 1.9.2,您可能会找到适合您的这两种解决方案之一。使用 render 代替 respond_with,如下所示。

解决方案 1

render :json => {:data => @data, :status => "whatever"}

json 结果是:

{
  data:
  {
    user:
    {
      address: null
      email: "[email protected]"
      first_name: "Test"
      last_name: "Man"
      status_id: 1
      username: "testguy"
    }
  }
  status: "whatever"
}

解决方案 2

render :json => {:user => {:address => @data.address, :email => @data.email, ..., :status => "whatever"}}

json 结果是:

{
  user:
  {
    address: null
    email: "[email protected]"
    ...
    status: "whatever"
  }
}

我希望这就是您正在寻找的内容。

I assume that the status you want to include is not one of the http status codes (200, 201, etc.) nor an object associated with the User object in any way and is your own variable.

Using Rails 3.0.10 and Ruby 1.9.2 you may have find one of these two solutions suitable for you. Instead of respond_with use render as follows.

Solution 1

render :json => {:data => @data, :status => "whatever"}

The json result is:

{
  data:
  {
    user:
    {
      address: null
      email: "[email protected]"
      first_name: "Test"
      last_name: "Man"
      status_id: 1
      username: "testguy"
    }
  }
  status: "whatever"
}

Solution 2

render :json => {:user => {:address => @data.address, :email => @data.email, ..., :status => "whatever"}}

The json result is:

{
  user:
  {
    address: null
    email: "[email protected]"
    ...
    status: "whatever"
  }
}

I hope this is what you were looking for.

星星的軌跡 2024-11-13 12:23:36

听起来您在使用内置 Rails 序列化时遇到了一些与我相同的痛苦。我最终使用了 RABL 模板,这让我的生活变得更加轻松。如果您需要快速启动和运行 RABL 的指南,我已经整理了一份:

http://blog.dcxn.com/2011/06/22/rails-json-templates-through-rabl/

It sounds like you're having some of the same suffering I experienced with using the built in rails serialization. I ended up using RABL templates which made my life a lot easier. If you need a guide on getting up and running with RABL quickly, I've put one together:

http://blog.dcxn.com/2011/06/22/rails-json-templates-through-rabl/

顾冷 2024-11-13 12:23:36

where 返回一个数组,因此您的结果有意义。您需要 where().first,或者由于它只是 ID 查找,因此只需使用 find 即可。

这是很久以前的事了,但这仍然是最简洁的答案:

@data = User.find(1)
respond_with(@data, :include => :status)

where returns an array, so your results make sense. You'd need where().first, or since it's just an ID lookup, simply use find.

This was a long time ago, but still, this is the most concise answer:

@data = User.find(1)
respond_with(@data, :include => :status)
清晨说晚安 2024-11-13 12:23:36

伙计,你现在可能已经解决了这个问题,但是如果你仍然遇到问题,如果你愿意的话,你可以这样做吗?

@data = User.find(1, :include => :status)

respond_to do |format|
  format.json do
    render @data.to_json(:include => :status)
  end
end

如果这不起作用,则您的关联可能有问题。 api 中有一个关于“关联的预加载”的非常好的部分:

http://apidock。 com/rails/ActiveRecord/Associations/ClassMethods

You might've solved it by now mate, but if you're still running into probs you can do it this way if you want?

@data = User.find(1, :include => :status)

respond_to do |format|
  format.json do
    render @data.to_json(:include => :status)
  end
end

If that doesn't work there may be something wrong with your associations. There's a really good section in the api on 'Eager loading of associations' here:

http://apidock.com/rails/ActiveRecord/Associations/ClassMethods

冬天旳寂寞 2024-11-13 12:23:36

正如 nixterrimus 指出的那样,RABL 确实是一条出路。它简单、干净、优雅。我对 Rails 比较陌生,没有使用过这些技术(to_json 等),但在做了一些研究并实际使用 RABL 后,我不禁想知道为什么它不是 Rails 的原生功能。

As nixterrimus pointed out RABL is really the way to go. It's simple, clean and elegant. I'm relatively new to rails and have not used these techniques (to_json, etc) but having done some research and actually using RABL, I cannot help but wonder why it's not a native feature of Rails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文