简体   繁体   中英

How to get current path/route in Sinatra?

I'd like to get the current path/route to use in an if statmenent in one of my views.

For example, I'd like to achieve something like this:

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

This way, two routes go to the same view, but if accessed via /hello , it will render an extra partial _hello.erb onto the view.

Is this possible in Sinatra? What's the best way to achieve it?

I believe you are going to want to use the request object. request.path_info should give you what you are looking for.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM