可以使用 Sinatra 重定向和显示内容吗? [英] Possible to redirect AND show content with Sinatra?
本文介绍了可以使用 Sinatra 重定向和显示内容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 Sinatra 路由进行重定向并显示重定向消息.所以
I'd like to do a redirect with a Sinatra route AND show a redirection message. So
http://api.mysite.com/product/123.html这样做
redirect product.url, 301
AND 输出
"redirecting..."
这可能吗?代码是什么样的?
Is this possible, and what would the code look like?
推荐答案
来自 Sinatraredirect
的文档:
From the Sinatra documentation for redirect
:
任何附加参数都像传递给halt
的参数一样处理:
Any additional parameters are handled like arguments passed to
halt
:
redirect to('/bar'), 303
redirect 'http://google.com', 'wrong place, buddy'
对于暂停
:
还可以指定暂停时的状态:
You can also specify the status when halting:
halt 410
或者身体:
halt 'this will be the body'
或两者兼而有之:
halt 401, 'go away!'
所以你可以这样做:
redirect product.url, 301, "redirecting..."
尽管 HTTP 规范说:
...响应的实体应该包含一个简短的超文本注释,带有指向新 URI 的超链接.
...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}..."
这篇关于可以使用 Sinatra 重定向和显示内容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文