【发布时间】:2017-03-22 06:56:09
【问题描述】:
我在使用 rails render_to_string 函数时遇到了困难。我已经使用 --api 标志创建了一个应用程序,所以我认为这可能是问题,因为我已经在“完整”rails 应用程序中进行了测试,并且它的工作正常。
基本上是我在打电话:
body_html = render_to_string(template: 'reservations/print')
我也试过了
body_html = render_to_string('reservations/print')
body_html = render_to_string(partial: 'reservations/print')
body_html = render_to_string(partial: 'reservations/print.html.erb')
应该返回该模板的 html。文件名是 print.html.erb 并且只有基本数据,即
<p> Hello world. </p>
当我输出 body_html 时它是空的。
我引用了这个 SO 问题 What is the correct way to render_to_string in wicked pdf?,并且我还将使用 Wicked PDF 生成一个 pdf 文件。
非常感谢
【问题讨论】:
-
引用,栈溢出问题。您是否尝试只传递
reservations/print而没有模板关键字? -
我认为
--api标志不是问题所在。根据 Rails Api 上的文档,AbstractController::Rendering是默认的,此模块包含render_to_string方法。 -
嗨,丹,感谢您抽出宝贵时间回答。我有这个,还有一些。我会更新问题。
-
抱歉再添加一条评论,如果可能的话,只是想要一些新鲜的眼睛
标签: ruby-on-rails render-to-string