[英]What is the use of request.referer?
I want to know what the following code does. 我想知道以下代码的作用。 What is the use of
request.referer
? request.referer
的用途是什么?
@board = request.referer['dashboard'] if request.referer
request.referer
gives you the previous URL or /
if none. request.referer
为您提供先前的URL或/
如果没有)。 It is usually used to redirect the user back to the previous page ( link ) 通常用于将用户重定向回上一页( link )
Regarding your question, it is simply returning 'dashboard'
if found in request.referer
. 关于您的问题,如果在
request.referer
找到,它只是返回'dashboard'
。 Look at the following example: 看下面的例子:
> str = "hello world!"
=> "hello world!"
> str['hello']
=> "hello"
> str['lo wo']
=> "lo wo"
> str['foo']
=> nil
However, you should not depend on this method to redirect your user back. 但是,您不应依赖此方法将用户重定向回。 You can do this in your controller instead:
您可以在控制器中执行以下操作:
redirect_to :back
request.referer gives you the previous URL or /
if none request.referer为您提供先前的URL或
/
如果没有)
In library you can see: 在库中,您可以看到:
def referer
@env['HTTP_REFERER'] || '/'
end
You can use the referer technique for this, but you'll have to capture it when entering the form instead of when the form is submitted. 您可以为此使用引用技术,但是在输入表单时(而不是在提交表单时)必须捕获它。 Something like this:
像这样:
<%= hidden_field_tag :referer, (params[:referer] || request.env['HTTP_REFERER']) %>
Then you can use params[:referer]
in the controller to redirect back
. 然后,您可以在控制器中使用
params[:referer]
进行redirect back
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.