加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – JBuilder从视图调用to_builder

发布时间:2020-12-17 02:24:54 所属栏目:百科 来源:网络整理
导读:我喜欢为每个模型定义to_builder方法并在视图中使用它的想法.但是,当我从视图中调用它时,它返回一个字符串而不是一个对象.如何在视图中使用通用的to_builder方法并返回一个对象而不是在视图中使用实例.(self,* self.attributes.keys)? # doctor should be a
我喜欢为每个模型定义to_builder方法并在视图中使用它的想法.但是,当我从视图中调用它时,它返回一个字符串而不是一个对象.如何在视图中使用通用的to_builder方法并返回一个对象而不是在视图中使用实例.(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

解决方法

使用合并!:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读