Ruby 如何使用Sinatra呈现普通HTML文件?

Ruby 如何使用Sinatra呈现普通HTML文件?,ruby,sinatra,Ruby,Sinatra,我有一个简单的sinatra应用程序。我所要做的就是将其用作包装器,以特定的路径为静态HTML文件提供服务。我的目录结构如下所示: /directory myhtmlfile.html app.rb require 'sinatra' get '/myspecialroute' do html :myhtmlfile # i know html is not a method, but this is what I would like to do end

我有一个简单的sinatra应用程序。我所要做的就是将其用作包装器,以特定的路径为静态HTML文件提供服务。我的目录结构如下所示:

/directory
    myhtmlfile.html
    app.rb
require 'sinatra'

get '/myspecialroute' do
    html :myhtmlfile      # i know html is not a method, but this is what I would like to do
end
get '/myspecialroute' do
  redirect '/myspecialroute.html'
end
我的
app.rb
文件如下所示:

/directory
    myhtmlfile.html
    app.rb
require 'sinatra'

get '/myspecialroute' do
    html :myhtmlfile      # i know html is not a method, but this is what I would like to do
end
get '/myspecialroute' do
  redirect '/myspecialroute.html'
end
我如何编写它,使我的html文件保持一个普通的html文件,但以特殊的方式提供它

解决方案: 多亏了,我学会了几种不同的方法:

get '/myspecialroute' do
  File.read('myhtmlfile.html')
end
这将打开、读取、关闭文件,然后以字符串形式返回文件

或者,有一个帮助函数可以使此更干净:

get '/myspecialroute' do
  send_file 'myhtmlfile.html'
end

您可以这样做:

/directory
    myhtmlfile.html
    app.rb
require 'sinatra'

get '/myspecialroute' do
    html :myhtmlfile      # i know html is not a method, but this is what I would like to do
end
get '/myspecialroute' do
  redirect '/myspecialroute.html'
end
你想做什么就做什么

e、 g

这是我的工作:

require 'rubygems'
require 'sinatra'

get '/index.html' do
    @page_title = 'Home'
    @page_id = 'index.html'
    erb :'index.html', { :layout => :'layout.html' }
end

您可以将其更改为.erb,然后调用其中没有erb的erb方法。您希望它在请求
'/myspecialroute'
'/myspecialroute.HTML'
时呈现HTML文件吗?@tbuehlmann我希望该路由保持
/myspecialroute
@marnenbow-Koser在服务器上启动零配置web服务器可用端口。还要添加一条特殊路线。你有别的选择吗?韦德里克呢?我知道这似乎有些过时,但这可能是你在这里需要的。看,这会导致重定向。我想停留在最初请求的路径上。有没有办法加载文件内容并返回它?看这个