Ruby-on-rails 路由到 /public 中的静态 html 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5631145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Routing to static html page in /public
提问by Aen Tan
How can I route /footo display /public/foo.htmlin Rails?
如何路由/foo以/public/foo.html在 Rails 中显示?
回答by Arkan
You can do this:
你可以这样做:
Add this, into your routes.rb file.
将此添加到您的 routes.rb 文件中。
match '/foo', :to => redirect('/foo.html')
Update
更新
In Rails 4, it should use "get", not "match":
在 Rails 4 中,它应该使用“get”,而不是“match”:
get '/foo', :to => redirect('/foo.html')
thanks Grant Birchmeier
感谢格兰特·伯奇迈尔
回答by Eliot Sykes
This can be done without triggering a redirect. Follow the steps further down to be able to route static files in config/routes.rbas shown in this example:
这可以在不触发重定向的情况下完成。按照进一步向下的步骤来路由静态文件,config/routes.rb如下例所示:
# This route will serve public/index.html at the /login URL
# path, and have a URL helper named `login_path`:
get "/login", to: static("index.html")
# This route will serve public/register.html at the /register
# URL path, and have URL helper named `new_user_registration_path`:
get "/register", to: static("register.html"), as: :new_user_registration
- Install the rails-static-router gem: https://github.com/mufid/rails-static-router#installation
- Restart app (first
bin/spring stopto be sure app is completely reloaded). - Start using the
static(path)method in yourconfig/routes.rb.
- 安装 rails-static-router gem:https: //github.com/mufid/rails-static-router#installation
- 重新启动应用程序(首先
bin/spring stop要确保应用程序已完全重新加载)。 - 开始使用
static(path)您的config/routes.rb.
回答by Александр Тихонович
E.g in Rails 4 add the following route:
例如在 Rails 4 中添加以下路由:
get '/example', :to => redirect('example.html')
Also you need to enable static files from the 'public' directory in your configuration:
您还需要从配置中的“public”目录启用静态文件:
config.serve_static_files = true
OR
或者
config.serve_static_assets = true
Also you might need to provide your public directory as root in NGINX configuration.
此外,您可能需要在 NGINX 配置中以 root 身份提供您的公共目录。

