Possible to redirect AND show content with Sinatra?

为君一笑 提交于 2019-12-13 04:14:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!