【发布时间】:2015-11-16 02:09:49
【问题描述】:
我正在尝试在 Rails 5.0.0.alpha 中做一些非常简单的事情:渲染不在 public 目录中的 HTML 文件。
我的路线是这样的:
get '/api/docs' => 'static#documentation'
而我的StaticController 看起来像这样:
class StaticController < ApplicationController
def documentation
render 'api/documentation/index.html'
end
end
我要渲染的index.html文件位于app/views/api/documentation/index.html。
每当我在浏览器中加载/api/docs 时,我得到的只是一个带有空白页面的200 OK 响应,这意味着没有呈现任何内容。
我几乎尝试了render 的所有变体:
render 'app/views/api/documentation/index.html'render 'views/api/documentation/index.html'render 'api/documentation/index.html'render '/documentation/index.html'render 'app/views/api/documentation/index'render 'views/api/documentation/index'render 'api/documentation/index'render '/documentation/index'render file: 'app/views/api/documentation/index'render file: 'views/api/documentation/index'render file: 'api/documentation/index'render file: '/documentation/index'
等等。我错过了什么?
编辑:服务器输出:
Started GET "/api/docs" for 198.167.XXX.XX at 2015-11-16 02:07:48 +0000
Processing by StaticController#documentation as HTML
Completed 200 OK in 1ms (Views: 0.2ms | ActiveRecord: 0.0ms)
【问题讨论】:
-
服务器输出是什么?
-
@Jason 查看我的编辑。就像我在问题中所说,这是一个
200 OK响应,没有错误... -
我看到您在问题中提到了这一点,但查看完整的服务器输出显示我们缺少
Rendered api/documentation/index.html行。此外,您可以确定它是否找到文件的一种方法是重命名它并查看是否收到“模板丢失”错误。如果这不会引发错误,那么您可能遇到了路径问题。如果您在重命名文件时确实收到该错误,那么它实际上是在查找文件。在这种情况下,请尝试puts render_to_string('api/documentation/index.html')并查看文件内容是否显示在服务器输出中。
标签: ruby-on-rails