0

I'm currently using pundit to authorise my controller methods.

after_action :verify_authorized

I am also currently use Bugsnag to catch unhandled errors.

I have an application with relatively ephemeral records used by a large audience and often I receive multiple unhandled errors where the record being looked up is no longer present (typically by users unintentionally navigating back to a browser auto-completed URL).

Currently my solution is doing this for each method in my controller:

def method_name
  begin
    authorize @record
  rescue
    if @record.nil?
      redirect_to missing_record_path
    else
      redirect_to :back, notice: 'You are not authorised to view this record.'
    end
  end

  # rest of code goes here
end

There's a lot of code repetition and I'm sure there's a smarter way to handle this whilst also support as an after_action command. What do you think?

  • Surely you'll get a missing record error raised before Pundit is even involved? – Jon Jun 28 '21 at 16:27
  • Let's have `before_action :record_finder ` to check for record existence. e.g `def record_finder @record = Record.find_by(attr: params[:attr]) return redirect_to missing_record_path unless @record.present? end` – Ali Ammaar Jun 28 '21 at 21:02

0 Answers0