Rails 将任何路径路由到 public 中的静态文件

Rails route any path to static file in the public

我正在构建静态页面应用程序。

每个路径查询,/ (root), /sessions/new, /posts/ 应该 return 文件放在 public/index.html 没有重定向(我需要保存路径,因为内部 javascripts 模块可以使用它。

我该怎么做?

添加一个 after_action 可能会起作用,这将被继承到所有控制器,从而允许每个控制器方法呈现此文件。我不认为你可以直接在 routes.rb 中提供静态文件(如果有人不知道,请纠正我)

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  # ...
  # ...

  after_action render_index

  def render_index
    render file: 'index.html'
  end

  # ...
  # ...
end

桓子说得对,没有控制器就没有机会。

我发现的最好方法 - 是编写一个控制器来发送放置在 public 文件夹中的静态文件。