在rails之外渲染html文件

时间:2014-11-29 19:00:38

标签: ruby-on-rails

我正在尝试渲染一个不在rails项目中的html文件,但是使用rails作为路径。

我一直在尝试设置这样的路径,但它会尝试链接到公用文件夹中的文件。

get '/pathtofile', :to => redirect("/path/to/the/other/file.html")

我也试过像这样在控制器中渲染另一个文件,但没有运气。

render "/path/to/the/other/file.html"

我有什么方法可以做到这一点吗?

1 个答案:

答案 0 :(得分:0)

您可以通过多种方式自定义Rails用于查找视图模板的路径,包括:

# example one
class ProjectsController < ApplicationController
  prepend_view_path 'app/views/mycustomfolder'

# example two
class ProjectsController < ApplicationController
  def self.controller_path
    "mycustomfolder"
  end

理论上你可以使用它来完全退出应用程序,这就是你要求做的事情:

class ProjectsController < ApplicationController
  prepend_view_path 'app/../..' # you are now in the parent folder of the app itself

但我认为这将是一个脆弱的解决方案。例如,当您托管应用时,链接到的文件不会使用它,您的链接也会中断。该应用程序由其根文件夹定义,它最适合您在应用程序中链接到的所有内容,或者位于应用程序的文件夹树中,或者可以通过URI访问。