指定格式:“xml”使用 render_to_string 忽略

发布于 2024-12-23 18:39:11 字数 426 浏览 8 评论 0原文

我有一个需要将视图渲染为字符串的操作。该视图称为index.xml.erb。我正在尝试使用 render_to_string 来实现此目的:

my_string = render_to_string(layout: false, format: "xml")

render_to_string 是渲染 index.html.erb 的内容并将其分配给 my_string。我缺少什么?

注意:我知道我可以做这样的事情:

my_string = render_to_string(:action => "#{self.action_name}.xml.erb")

但我很好奇为什么 render_to_string 不支持“format”选项。

I have an action that needs to render a view to string. The view is called index.xml.erb. I am trying to achieve this with render_to_string:

my_string = render_to_string(layout: false, format: "xml")

render_to_string is instead rendering the contents of index.html.erb and assigning it to my_string. What am I missing?

Note: I am aware that I can do something like this:

my_string = render_to_string(:action => "#{self.action_name}.xml.erb")

But I'm curious as to why the "format" option isn't honored with render_to_string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2024-12-30 18:39:11

这对我有用。

render_to_string( :action => "#{self.action_name}", :formats => [:xml] )

This works for me.

render_to_string( :action => "#{self.action_name}", :formats => [:xml] )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文