Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 719
f7n Avatar asked Feb 23 '16 01:02

f7n


1 Answers

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

like image 168
Justin Wood Avatar answered Oct 14 '22 03:10

Justin Wood