Search code examples
ruby-on-railsruby-on-rails-3authorizationcancan

How to show AccessDenied errors on the active page with CanCan in Rails3


I am trying to find a way to display my flash errors on the active page without the redirect_to method.

By the way, the standard flash[:alert] = exception.message didn't show me any error messages, so I changed it to flash[:error].

Thanks for any advice!

rescue_from CanCan::AccessDenied do |exception|  
    flash[:error] = exception.message  
    redirect_to deadlines_path  
end

Solution

  • Try:

    rescue_from CanCan::AccessDenied do |exception|
      flash.now[:alert] = exception.message
      render 'something_else'
      return false
    end
    

    without a redirect.