我喜欢为每个模型定义一个 to_builder
方法并在 View 中使用它的想法。但是,当我从 View 中调用它时,它返回一个字符串而不是一个对象。如何在 View 中使用通用的 to_builder 方法并返回一个对象,而不是在 View 中使用 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
最佳答案
使用merge!
:
json.merge! rel.patient_profile.to_builder.attributes!
关于ruby-on-rails - JBuilder 从 View 调用 to_builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32685502/