ruby-on-rails - JBuilder 从 View 调用 to_builder

标签 ruby-on-rails ruby jbuilder

我喜欢为每个模型定义一个 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!

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

关于ruby-on-rails - JBuilder 从 View 调用 to_builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32685502/

相关文章:

ruby-on-rails - 如何在 Ruby Sunspot 中定义动态作用域?

ruby-on-rails - 如何使用 Rails 检查时间是否在范围内

ruby - 如何在 Sinatra 中为 JSON 定义全局错误处理程序

ruby - opscode Chef 可以执行等待直到条件成立吗?

ruby-on-rails - Rails jbuilder 缓存+查询

ruby-on-rails - Rails will_paginate 在同一页面上的两个对象上不起作用

ruby-on-rails - 如何使用Rspec检查方法是否存在?

接受对象或其 id 作为参数的 Ruby 语义

ruby-on-rails - 我如何在 Jbuilder 中使用 Rails 助手和 Gon gem?

javac - 如何增加 Borland JBuilder 2005/2006 中 javac 进程的最大堆大小