简体   繁体   中英

What is the use of request.referer?

I want to know what the following code does. What is the use of request.referer ?

@board = request.referer['dashboard'] if request.referer

request.referer gives you the previous URL or / if none. It is usually used to redirect the user back to the previous page ( link )

More information here

Regarding your question, it is simply returning 'dashboard' if found in request.referer . 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

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 .

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