简体   繁体   中英

How to redirect back to a route in Sinatra?

In Sinatra, how to redirect back to a route?

require 'sinatra'

get "/A" do
  redirect '/B' 
end

get "/B" do 
  redirect request.referer
end

This is the code, and I want "/B" bring me back to "/A".

Now, in browser, in "/C", send a get request to "/A". I will then go back to "/C". The reason is:
The "request.referer" is recording "/C", because the redirect in "/A" is initiated by the server, it doesn't count as the referrer to the target page.

There is a JavaScript way solving it(placed on /A):

<script type="text/javascript">window.location = '/B';</script>

(Got this from: How to redirect web page from a specfic page in Sinatra? )

My question is how to redirect back to a route in server side?

Just say redirect back . From the Sinatra README (scroll down to "Browser Redirect"):

You can also easily redirect back to the page the user came from with redirect back:

get '/foo' do
  "<a href='/bar'>do something</a>"
end

get '/bar' do
  do_something
  redirect back
end

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