简体   繁体   中英

Rails error: No HTTP_REFERER was set in the request to this action

I am developing a new rails app. but found a error:

ActionController::RedirectBackError in UsersController#show

No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].

my code:

rescue_from CanCan::AccessDenied do |exception|
     redirect_to :back
end

I have some question for this error:

  • what is HTTP_REFERER?
  • why redirect to back will trigger this error?

Anyone has good idea?

what is HTTP_REFERER?

The HTTP referer is an HTTP header field that identifies the address of the webpage (ie the URI or IRI) that linked to the resource being requested. This is set by ActionController::Request object.

why redirect to back will trigger this error?

This usually happens when request.env["HTTP_REFERER"] is not set. (I also wonder to know in which case it is set and not set)

You could refer this answer to fix your issue.

(OR) I would highly prefer to define a custom page for access denied and redirect to it instead of redirecting :back (which I think bad idea)

For example from cancan gem docs,

rescue_from CanCan::AccessDenied do |exception|
    render :file => "#{Rails.root}/public/403.html", :status => 403, :layout => false
    ## to avoid deprecation warnings with Rails 3.2.x (and incidentally using Ruby 1.9.3 hash syntax)
    ## this render call should be:
    # render file: "#{Rails.root}/public/403", formats: [:html], status: 403, layout: false
end

Hope this helps!

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