Search code examples
ruby-on-railsrubyauthorizationrenderbefore-filter

rails: put and interruption in before filter


I want a before filter like "must_have_permission_to_write" that when called if user hasn't permission to write renders a message saying "you can't do that!" and return.

Problem is I'm getting "can only render or redirect once per action" of course... how can I stop the execution in the before filter? thanks


Solution

  • I think the easiest answer is add a redirect and return false to your must_have_permission_to_write method.

    def must_have_permission_to_write
      unless current_user.has_permission?(something)
        redirect_to access_denied_path 
        return false
      end
    end
    

    Then, create an action for access denied somewhere, add the route, and put whatever you want in the template.