ActiveAdmin obscuring error messages in forms

23 Views Asked by At

We are having an issue with ImageMagick that we cannot get sorted but we need to obscure the error relating to it from the user in an ActiveAdmin form with an image upload. I have come up with a brute-force action override which works for updating, but breaks for creating. This I know is not ideal but I am having a very difficult time deciphering the source code. Is there a better way?

controller do
    before_action :set_topic,
                  only: [:create, :update]

    def create
      unless create_or_update_topic
        super
      end
    end

    def update
      unless create_or_update_topic
        super
      end
    end

    private

    def set_topic
      topic_id = params[:id]
      @topic = topic_id.present? ? Topic.find(topic_id) : nil
    end

    def create_or_update_topic
      begin
        if @topic.nil?
          @topic = Topic.create!(permitted_params[:topic])
        else
          @topic.update!(permitted_params[:topic])
        end

        respond_to do |format|
          format.html { redirect_to admin_topic_path(@topic), notice: "Topic was successfully #{params[:action] === 'create' ? 'created' : 'updated'}." }
        end
      rescue
        if @topic.errors.messages.values.flatten.any? {|v| v.include?('Paperclip::Errors'||'ImageMagick')}
          error_message = "There was an issue with uploading your image file."
        else
          return false
        end
        respond_to do |format|
          if params[:action] === 'update'
            format.html { redirect_to edit_admin_topic_path(@topic), flash: { error:  error_message} }
          else
            format.html { redirect_to new_admin_topic_path, flash: { error:  error_message} }
          end
        end
      end
    end
  end

Override the action, catch the error I want to obscure, redirect to the form with flash message. Leave built-in error handling in place by deferring to super if the error I am concerned with is not present.

Works for updating when submitting a corrupted image file, but responds with a 500 error when doing the same from the create form.

1

There are 1 best solutions below

0
Eleventy On

Not sure how I overlooked this in the ActiveAdmin readme, but the following is exactly what I needed. It's still a brute-force way of achieving my goal of obscuring the specific ImageMagick error, and gets dinged by rubocop for directly altering errors, but it works nicely with AA's built-in error handling:

controller do
    def create(_options={}, &block)
      create! do |success, failure|
        yield(success, failure) if block

        resource.errors.messages.each do |k,v|
          if v.include?("Paperclip::Errors::NotIdentifiedByImageMagickError")
            resource.errors.messages[k] = "There was an issue with uploading your image file."
          end
        end

        failure.html { render :new }
      end
    end

    def update(_options={}, &block)
      update! do |success, failure|
        yield(success, failure) if block

        resource.errors.messages.each do |k,v|
          if v.include?("Paperclip::Errors::NotIdentifiedByImageMagickError")
            resource.errors.messages[k] = "There was an issue with uploading your image file."
          end
        end

        failure.html { render :edit }
      end
    end
  end
end