【问题标题】:Rails 4 render json nested objectsRails 4 渲染 json 嵌套对象
【发布时间】:2013-12-17 16:03:30
【问题描述】:

我需要将复杂的结构呈现为 Json。我有下一个结构工作:

render :json => @booking, :include => [:paypal, 
                                       :boat_people,
                                       :boat => {:only => :boat_model, :include => {:boat_model => {:only => :name, :include => { :boat_type => {:only => :name}}}}}] 

但我无法将带有其他一些嵌套属性的端口属性添加到 :boat,例如 :boat_model (在同一级别)。

更新:

虽然它不起作用,但我包含了我的端口属性。

render :json => @booking, :include => [:paypal, 
                                       :boat_people,
                                       :boat => {:only => [:boat_model => {:include => {:boat_model => {:only => :name, :include => { :boat_type => {:only => :name}}}}},
                                                           :port => {:include => :city => {:only => name}}]}]

我的意思是,boat_model 和 port 都是船属性。

这是模型对象:

class Boat < ActiveRecord::Base

  attr_accessor :price
  @price

  attr_accessor :extrasPrice
  @extrasPrice

  def as_json(options = { })
    h = super(options)
    h[:price] = @price    
    h[:extrasPrice] = @extrasPrice
    h
  end


  belongs_to :boat_model
  belongs_to :port
  belongs_to :state
  has_many :photos
end

【问题讨论】:

  • 您能否包含您尝试添加的内容以添加带有嵌套属性的端口?另外,您与 Port 的关系如何?
  • @Joe Edgar 查看更新。

标签: ruby-on-rails ruby json


【解决方案1】:

我明白了。

render :json => @booking, :include => [:paypal, 
                                       :boat_people,
                                       :boat => {:only => :name, :include => {:port => {:only => :name, :include => {:city => {:only => :name, :include => {:country => {:only => :name}}}}}, 
                                                :boat_model => {:only => :name, :include => {:boat_type => {:only => :name}}}}}]

【讨论】:

    【解决方案2】:

    您可能需要一个更强大的系统来显示 JSON。内置的 Rails 帮助器实际上主要是为简单的添加而设计的,使用户能够完成他们想要完成的大部分工作。但是,在您的情况下,您正试图让它做的比设计的要多。

    我强烈建议创建 view object 或使用像 RABL 这样的 gem。

    我的偏好是将 Rabl 用于复杂的 JSON。它基本上通过构建特定领域的语言为您创建“视图对象”,这使得在 Rails 中构建复杂的 JSON 对象变得相对容易。

    RABL 基本上允许您构建格式为 JSON 而不是 HTML 的视图。 DSL 非常丰富,可以让您做任何您想做的事情。在你的情况下,我认为代码看起来像这样:

    app/views/bookings/show.rabl
    
    object @booking
    #these are attributes that exist on your booking model: 
    attributes :booking_attribute, :other_booking_attribute
    
    child :paypal do
      #these are attributes that exist on your paypal model:
      attributes :paypay_attribute1, :other_paypal_attribute
    end
    
    child :boat_people do
      #boat_people attributes that you want to include
      attributes :blah_blah
    end
    
    child :boat do
      #boat attributes that you want to include
      attributes :boat_attribute1, :boat_attribute2
    
      child :boat_model do
        attributes :name
    
        child :boat_type do
          attributes :name
        end
      end
    
    end
    

    【讨论】:

    • 那么,用上面的方法是不行的吗?
    • 这是正确答案,rabl 是正确的方式渲染嵌套 jsons。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    相关资源
    最近更新 更多