Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lujanfernaud/8aeb42c92160f3c4744b2430bdd06701 to your computer and use it in GitHub Desktop.
Save lujanfernaud/8aeb42c92160f3c4744b2430bdd06701 to your computer and use it in GitHub Desktop.
Rails and Pundit: Custom Redirection When Not Authorized

Rails and Pundit: Custom Redirection When Not Authorized

To use a custom redirection when a resource is not authorized we only need to add an user_not_authorized method to the controller, and define the redirection.

Example:

class Groups::MembershipRequestsController < ApplicationController
  def new
    @membership_request = MembershipRequest.new
    @group = find_group

    authorize @membership_request
  end

  # (...)

  private
  
    def user_not_authorized
      redirect_to new_user_registration_path
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment