Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.