我正在使用 Rails 4 中的 JBuilder 呈现一些学生的 JSON。我希望每个学生都有一个“html”属性,其中包含给定学生的 HTML 部分:
[
{ html: "<b>I was rendered from a partial</b>" }
]
我尝试了以下方法:
json.array! @students do |student|
json.html render partial: 'students/_student', locals: { student: student }
end
但这给了我:
Missing partial students/_student with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :haml]}.
最佳答案
您必须指定部分格式,因为默认情况下,Rails 会查找当前格式 (json) 的部分格式。例如:
render partial: 'students/student.html.erb'
关于ruby-on-rails - 在 JSON JBuilder 中呈现 html 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19102160/