问题
I'd like to do a redirect with a Sinatra route AND show a redirection message. So
http://api.mysite.com/product/123.html would do this
redirect product.url, 301
AND output
"redirecting..."
Is this possible, and what would the code look like?
回答1:
From the Sinatra documentation for redirect:
Any additional parameters are handled like arguments passed to
halt
:redirect to('/bar'), 303 redirect 'http://google.com', 'wrong place, buddy'
and for halting:
You can also specify the status when halting:
halt 410
Or the body:
halt 'this will be the body'
Or both:
halt 401, 'go away!'
So you can do:
redirect product.url, 301, "redirecting..."
Although the HTTP spec says:
...the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).
so you might want to do:
redirect product.url, 301, "redirecting to #{product.url}..."
来源:https://stackoverflow.com/questions/18936716/possible-to-redirect-and-show-content-with-sinatra