c+静态html,路由到/ public中的静态html页面

这可以在不触发完成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值