【问题标题】:Render html file outside of rails在 Rails 之外渲染 html 文件
【发布时间】:2015-01-28 03:04:54
【问题描述】:

我正在尝试渲染不在 rails 项目中但使用 rails 路由的 html 文件。

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

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

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

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

有什么办法可以做到吗?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您可以通过多种方式自定义 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 访问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      • 2015-12-10
      • 2021-01-05
      • 2016-03-06
      • 1970-01-01
      • 2011-01-17
      相关资源
      最近更新 更多