这可以在不触发完成
get '/foo', :to => redirect('/foo.html')
感谢重定向。按照以下步骤进一步下降到能够在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
创建config/initializers/static_router.rb与这个答案的末尾显示文件的全部内容。确保您为与应用的Rails版本相关的行切换注释。
重新启动应用程序(首先bin/spring stop以确保应用程序完全重新加载)。
开始在您的config/routes.rb中使用static(path)方法。
# File: config/initializers/static_router.rb
module ActionDispatch
module Routing
class StaticResponder < Endpoint
attr_accessor :path, :file_handler
def initialize(path)
self.path = path
# Only if you're on Rails 5+:
self.file_handler = ActionDispatch::FileHandler.new(
Rails.configuration.paths["public"].first
)
# Only if you're on Rails 4.2:
# self.file_handler = ActionDispatch::FileHandler.new(
# Rails.configuration.paths["public"].first,
# Rails.configuration.static_cache_control
#)
end
def call(env)
env["PATH_INFO"] = @file_handler.match?(path)
@file_handler.call(env)
end
def inspect
"static('#{path}')"
end
end
class Mapper
def static(path)
StaticResponder.new(path)
end
end
end
end