Active scffold + paperclip, display document from S3

34 Views Asked by At

I'm using s3 to host my images and active scaffold gem to deal with the admin part of my app. I'm currently facing the following problem:

Considering the following show action:

def show
  @record = find_if_allowed(params[:id], :read)
  do_show
  send_data(@record.document_file_content)
end

I can't display my image correctly, clicking the show of a s3 image results in weird encrypted characters such as:

�PNG  IHDRVO �iCCPICC ProfileH��WXS��[R  -)�w�W�#l�$@(��]YTp�"��� 
��ņ]Y{XPYY6Tޤ���}��ͽ?g�9�s�@ц����*��/��2��S��G�*�(���|���pe��wyw�B�j%�����U�9\!$�4����!puv��B��*��!�U�
D\�3�X]�Ӥ�Rb��7d*�%�@A̛Y�΀q�m���=ٙ,�� ���̓X�...

Since the bucket is configured correctly, I assume it must be a way to display this content as an image tag. I tried overwritting the show view for this controller but was unsuccessful.

Have you run into this problem before? If yes, what would be the best option for me?

Kind regards

1

There are 1 best solutions below

0
gastngouron On

I had to make an Helper such as:

module Admin::DocumentHelper

  def document_document_column(record, opts={})
    str = ""
    if record.document.file?
      if record.document_content_type.match(/pdf/)
        str << content_tag(:iframe, record.document.original_filename, style: 'border:none;', width: 400, height: 250, src: admin_document_path(record, :style => :original, disposition: :inline) )
      else
        str << content_tag(:div, class: 'img-document', style: "display: table-cell; vertical-align: middle; cursor: pointer; width: 400px; height: 400px; overflow-x: hidden; overflow-y:auto") do
          image_tag(admin_document_path(record, :style => :original, disposition: :inline), style: 'border:none;max-height: 400px; width:400px')
        end
      end
      str << link_to("download", admin_document_path(record, :style => :original, disposition: :inline), target: "_blank")
    end
    str.html_safe
  end

end