Render a JBuilder view in html view

守給你的承諾、 提交于 2019-12-03 02:38:38

render, when called from within a view, returns a string rendering of the passed template or partial; you can embed that string into your view as you like. Note though that:

  • You have to append your template name with the type suffix/extension. If you don't, Rails may get confused about which template file you're calling; ie: it might choose list.html.erb instead of list.json.jbuilder. If you're making this call from list.html.erb, trying to render list.html.erb results in infinite recursion and a SystemStackError. Using the :format option for render doesn't appear to work.
  • You have to specify the qualified path to the template; it won't find the right template for "list.json" just because list.json.jbuilder resides in the same directory as list.html.erb.
  • You need to pass the output of the render call through raw; otherwise, it will get escaped when it gets embedded into the view.

So, for your example, you might write this, assuming your templates were in /app/views/foo:

<div data-list="<%= raw render(:template => "foo/list.json", :locals => { :contents => @contents }) %>"></div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!