Cancan redirect back at AccessDenied

Hello

I have a rails 3 app and I am trying to implement the redirect back
action at access denied for cancan.

If I try this in my application_controller:
rescue_from CanCan::AccessDenied do |exception|
redirect_to :back
end

It gives the following error:
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”].

I can’t really understand why there is no http_referer, how is back
button working on views? Is it possible to implement a general back
action for the cancan access denied exception?

I like the devise approach of “back” method after sign in:
def after_sign_in_path_for(resource)
stored_location_for(resource)
end

It would be perfect if I could combine them.

Thank you!

If your request does not have the HTTP_REFERER defined,
redirect_to :back does not know where to redirect.
Ususally in this case root_url is used.
I hope, it will not be too difficult for you to combine

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs