如何路由/foo
以/public/foo.html
在Rails中显示?
Answers:
你可以这样做:
将此添加到您的routes.rb文件中。
match '/foo', :to => redirect('/foo.html')
更新资料
在Rails 4中,它应该使用“ get”,而不是“ match”:
get '/foo', :to => redirect('/foo.html')
redirect('/foo.html')
(不使用/ public)
get '/foo', :to => redirect('/foo.html')
(“ get”而不是“ match”)。
无需触发重定向即可完成此操作。按照下面的步骤进行操作,以便能够路由静态文件,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
bin/spring stop
确保应用程序已完全重新加载)。static(path)
方法config/routes.rb
。ActionDispatch::FileHandler.new(Rails.configuration.paths["public"].first)
require 'action_dispatch/middleware/static'
最高的
例如在Rails 4中添加以下路线:
get '/example', :to => redirect('example.html')
另外,您还需要在配置中启用“ public”目录中的静态文件:
config.serve_static_files = true
要么
config.serve_static_assets = true
另外,您可能需要在NGINX配置中以root用户身份提供公共目录。