如何在 Sinatra 中获取当前 path/route?

How to get current path/route in Sinatra?

我想让当前 path/route 在我的一个视图中的 if statmenent 中使用。

例如,我想实现这样的目标:

example.erb:

<h4>Foobar<h4>
<p>foo bar foo bar foo bar</p>
<% if current_route == /hello %>
  <%= erb :_hello %>
<% end %>

app.rb:

get '/foobar' do
  erb :example
end

get '/hello' do
  erb :example
end

这样,两条路线会到达同一个视图,但如果通过 /hello 访问,它将在视图上呈现额外的部分 _hello.erb

这在 Sinatra 中可行吗?实现它的最佳方法是什么?

我相信您会想要使用 request 对象。 request.path_info 应该会给你想要的东西。

http://www.sinatrarb.com/faq.html#path_info