Search code examples
ruby-on-railsfile-uploadamazon-s3shrine

Render a Markdown file stored in S3 uploaded with shrine - Rails


I'm using shrine to upload files from my rails application to S3. All is working fine, but I dont know how to display that file using redcarpet gem.

For example I can do this:

<div>
    <%= markdown("##title
    * ") %>
</div>

And works fine.

But if I do this:

<%= markdown(@rfile.rfile.url) %>

Is showing me a download link from S3.

How I can get the file content and not the file link?


Solution

  • Call to @rfile.rfile returns a Shrine::UploadedFile object, which has many more convenient methods other than just #url. On such method is #read, which retrieves content of the file:

    <%= markdown(@rfile.rfile.read) %>
    

    However, in this case the file would be opened and read, but not closed. So it's better to call #open with a block, and call #read on the yielded IO object, which can be neatly written as

    <%= markdown(@rfile.rfile.open(&:read)) %>