辛纳特拉地图绘制路线

发布于 2024-12-12 23:50:16 字数 525 浏览 1 评论 0原文

所有,我需要按照以下方式执行此操作,

require 'sinatra'
require 'app_env'

get '/home' do
  'home page'
end

get '/about' do
  'about page'
end

get '/docs' do
  'docs page'
end

我不确定哪条路线将是根路线,可能是主页、关于或文档页面。因此,我必须在文件 app_env.rb 中使用这样的行设置根页面 route_map '/home' => '/' 。 现在,我如何编写方法/函数 route_map

或其他任何内容来实现动态映射路线的要求。

编辑:我的答案

HOMEPAGE = '/home_page'
get '/' do
    status, headers, body = call! env.merge("PATH_INFO" => HOMEPAGE)
end

all, i need to do this as the following

require 'sinatra'
require 'app_env'

get '/home' do
  'home page'
end

get '/about' do
  'about page'
end

get '/docs' do
  'docs page'
end

I not sure which route will be the root route, maybe the home, about, or docs page. So, i have to set the root page in a file app_env.rb with a line like this route_map '/home' => '/' .
Now, how do i write the method/function route_map

or anything else to implement my requirement for mapping the route dynamically.

EDIT : MY ANSWER

HOMEPAGE = '/home_page'
get '/' do
    status, headers, body = call! env.merge("PATH_INFO" => HOMEPAGE)
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苯莒 2024-12-19 23:50:16

如果您希望将“/”的请求重新路由到“/home”,那么您需要做的就是:

get '/'
  redirect '/home'
end

如果您不想重定向,可以执行以下操作:

get '/foo' do
    status, headers, body = call env.merge("PATH_INFO" => '/bar')
    [status, headers, body.map(&:upcase)]
end

get '/bar' do
    "bar"
end

请参阅 了解详细信息。

If you are looking to reroute requests for '/' to '/home' then all that you need to do is:

get '/'
  redirect '/home'
end

If you do not want to redirect you could do this:

get '/foo' do
    status, headers, body = call env.merge("PATH_INFO" => '/bar')
    [status, headers, body.map(&:upcase)]
end

get '/bar' do
    "bar"
end

See this for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文