从 Controller 的方法,我试图捕获从另一个 Controller 渲染模板的输出。
View 文件位于此处:
path/to/show.html.erb
在我的 Controller 中,我有:
def create
html_string = render_to_string :template => 'path/to/show'
raise 'html string is empty!' if html_string.empty?
# do some other stuff
end
View 文件非常简单,仅包含文本“foo”。
我没有得到关于Rails找不到show.html.erb文件的任何错误,但是
html_string为空,并且引发错误。
通过rspec Controller 测试运行create方法时,我看到此行为。我还没有尝试通过Rails服务器的代码。
有人看到我在这里想念的东西吗?
最佳答案
事实证明这是由于我的 Controller 规范缺少重要信息:
render_views
关于ruby-on-rails - rails 3.1 render_to_string返回空字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8334249/