【发布时间】: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