Rails render_to_string 在部分视图中给出错误
在代码下方使用带有部分视图的 render_to_string 方法时,我收到 ActionView::MissingTemplate
错误
bizz = render_to_string(:partial => "biz_new",:layout => false)
即使我已明确指定 :layout => false
,我总是收到 MissingTemplate 错误。
但使用普通视图的 render_to_string 在同一项目中工作得很好。可能是什么原因?
在堆栈跟踪下方
ActionView::MissingTemplate(缺少部分商家/biz_new {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:text, 视图路径中的 "/"], :locale=>[:en, :en]} “/home/ramesh/works/xxx/app/views”, “/home/ramesh/works/xxx/vendor/plugins/asset_packager/app/views”):
I am getting ActionView::MissingTemplate
error when using render_to_string method with partial views, below the code
bizz = render_to_string(:partial => "biz_new",:layout => false)
Even though i have explicitly specified :layout => false
, i am getting the MissingTemplate error always.
But render_to_string with normal views works fine in the same project. what could be the reason?
below the stack trace
ActionView::MissingTemplate (Missing partial businesses/biz_new with
{:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:text,
"/"], :locale=>[:en, :en]} in view paths
"/home/ramesh/works/xxx/app/views",
"/home/ramesh/works/xxx/vendor/plugins/asset_packager/app/views"):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
render_to_string 需要起始下划线和 .html 扩展名。
Try
render_to_string needs the starting underscore and the .html extension.
正如 Mike Kijewski 提到的,您可以在部分名称的开头包含下划线,但如果您最后使用 .html,您将收到弃用警告。更直接的方法是这样的:
As Mike Kijewski mentioned, you can include the underscore at the beginning of the partial name, but if you use the .html in the end you will get a deprecation warning. A more straightforward way is this:
看起来 Rails 期望文件采用 txt 格式。文件名为什么?尝试命名它:
-或-
It looks like rails is expecting the file to be in format txt. What's the file named ? Try naming it:
-or-
我在使用
formats: "html"
但需要更改为formats: :html
之类的符号时遇到了问题。I had a problem where I was using
formats: "html"
but needed to be changed to a symbol likeformats: :html
.有类似的问题。
我找到了一个解决方案:
render_to_string(model, :formats => [:html])
Had similar issue.
I found a solution:
render_to_string(model, :formats => [:html])