0

I have used cancan and i have used

rescue_from CanCan::AccessDenied do |exception|  
  flash[:error] = "Access denied!"  
  redirect_to root_url  
end  

in my application controller but i want to stay on the same page where i perform that action and if possible a pop up window just showing

you are not authorised to perform this action

How to customize so as to stay in the same page wherever the unauthorized action is performed? please help me doing this.

logesh
  • 2,572
  • 4
  • 33
  • 60
  • Try this http://stackoverflow.com/questions/4767460/how-to-show-accessdenied-errors-on-the-active-page-with-cancan-in-rails3 – nishanthan Oct 25 '12 at 06:36

1 Answers1

2

Try redirect_to :back:

rescue_from CanCan::AccessDenied do |exception|  
  flash[:error] = "Access denied!"  
  redirect_to :back  
end
nickgrim
  • 5,387
  • 1
  • 22
  • 28
Rubyman
  • 874
  • 6
  • 15
  • This is what i was asking. Also can you tell me how to make an alert using popup showing you are not authorized and clicking on ok button has to put me in the same page. – logesh Oct 25 '12 at 09:42
  • you can show flash[:error] message on the same page or if you want alert popup, you can do it with javascript – Rubyman Oct 25 '12 at 09:48
  • ok. Thankyou so much. Also i have posted one more question in http://stackoverflow.com/questions/13047877/reset-password-link-asks-user-to-sign-in pls help me. – logesh Oct 25 '12 at 09:51
  • In Rails 5, we will use [redirect_back](http://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_back) like: redirect_back fallback_location: root_path – Mustafah Mar 21 '18 at 07:30