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

ruby-on-rails – 在JSON JBuilder中渲染html部分

发布时间:2020-12-16 19:37:06 所属栏目:百科 来源:网络整理
导读:我在Rails 4中使用JBuilder渲染一些学生的 JSON.我希望每个学生都有一个“html”属性,它包含给定学生的HTML部分: [ { html: "bI was rendered from a partial/b" }] 我已经尝试过以下内容: json.array! @students do |student| json.html render partial:
我在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'

(编辑:李大同)

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

    推荐文章
      热点阅读