Dud*_*olf 3 ruby-on-rails render-to-string
我在使用rails render_to_string函数时遇到困难。我已经使用--api标志创建了一个应用程序,因此我认为这可能是个问题,因为我已经在“完整”的Rails应用程序中对其进行了测试,并且效果很好。
本质上我在打电话:
body_html = render_to_string(template: 'reservations/print')
Run Code Online (Sandbox Code Playgroud)
我也尝试过
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')
Run Code Online (Sandbox Code Playgroud)
这应该返回该模板的html。文件名是print.html.erb,只有基本数据,即
<p> Hello world. </p>
Run Code Online (Sandbox Code Playgroud)
当我输出body_html时,它为空。
我引用了这个SO问题,在pdf格式中render_to_string的正确方法是什么?并且还将使用Wicked PDF生成pdf文件。
非常感谢
虽然我认为这不是完成以下工作的最佳解决方案。
ac = ActionController::Base.new()
html_string = ac.render_to_string :template => 'path_to_file' e.g. 'post/show'
Run Code Online (Sandbox Code Playgroud)
Rails API模式下的Application Controller文件是从ActionController :: API继承的,所以我猜要呈现的功能是在ActionController :: Base中。如果有人知道Base需要哪个模块,我将更新此答案吗?
谢谢鲁迪
| 归档时间: |
|
| 查看次数: |
5984 次 |
| 最近记录: |