JBuilder 中的嵌套关​​联 Rails

Nesting associations in JBuilder with Rails

我有以下 json jbuilder:

json.question_cluster @question_cluster

json.questions @question_cluster.questions do |question|
  json.id question.id
  json.title question.title
  json.required question.required
  json.has_other question.has_other
  json.position question.position
  json.options question.options do |option|
    json.id option.id
    json.label option.label
    json.value option.value
    json.position option.position
    json.go_page option.go_page
  end
end

在我的应用程序中生成以下响应:

问题是我想把问题放到question_cluster,但是@question_cluster是一个单独的对象,所以我不能用do end(会报错),怎么办在这种情况下做什么?

你试过这种方法吗?

json.question_cluster do
  json.(@question_cluster)
  json.questions @question_cluster.questions do |question|
    json.id question.id
    json.title question.title
    json.required question.required
    json.has_other question.has_other
    json.position question.position
    json.options question.options do |option|
      json.id option.id
      json.label option.label
      json.value option.value
      json.position option.position
      json.go_page option.go_page
    end
  end
end