【问题标题】:JBuilder call to_builder from viewsJBuilder 从视图调用 to_builder
【发布时间】:2015-12-17 13:54:28
【问题描述】:

我喜欢为每个模型定义一个to_builder 方法并在视图中使用它的想法。但是,当我从视图中调用它时,它会返回一个字符串而不是一个对象。如何在视图中使用通用的 to_builder 方法并返回一个对象,而不是在视图中使用instance.(self, *self.attributes.keys)

 # doctor should be an object, not a string representation of the object
 resp = [id: 1, content: "content", doctor: "{"id":245}" ]

  def to_builder
    Jbuilder.new do |instance|
      instance.(self, *self.attributes.keys)
    end
  end

json.medical_relationships @medical_relationships do |rel|
  json.(rel, *rel.attributes.keys)
  json.patient rel.patient_profile.to_builder.target!
  json.doctor rel.doctor_profile.to_builder.target!
end

【问题讨论】:

    标签: ruby-on-rails ruby jbuilder


    【解决方案1】:

    使用merge!:

    json.merge! rel.patient_profile.to_builder.attributes!
    

    在这里找到答案:https://stackoverflow.com/a/27335069/1319954

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-18
      • 2016-01-28
      • 2012-11-24
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      相关资源
      最近更新 更多