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?