指定格式为“xml”的渲染被忽略,使用了render_to_string。

19

我有一个需要将视图渲染成字符串的操作。该视图的名称为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”选项没有被采纳。

1个回答

28
最初的回答:这对我有效。 render_to_string(:action => "#{self.action_name}", :formats => [:xml])意思是将当前控制器的指定动作渲染成XML格式的字符串。

3
天啊,7年后这个答案帮我摆脱了头疼的问题,我一直在尝试从一个JSON请求中获取一些PDF模板。感谢您的贡献。 - RudyOnRails

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接