Vivek
Vivek

Reputation: 326

Problem accessing images uploaded to heroku thru RoR application

I am trying to upload a image from application and then trying to access it from another page. The code used for uploading the image is as follows:

uploaded_io = params[:recipe][:picture]                  
  File.open(Rails.root.join('recipe_images',uploaded_io.original_filename), 'wb') do |file|
  file.write(uploaded_io.read)
end

This works fine when on the local development environment, however when I load the application on Heroku and try to access the image I get the following error:

(No such file or directory @ rb_sysopen - /app/recipe_images/logo.jpeg):

I tried creating a folder recipe_images in the app folder but again get the same issue.

For accessing the uploaded image I am using the following code in my view:

<%= image_tag("recipe_images/logo.jpeg"%>

This again works on the development environment but fails on production since the image is not there.

Why isn't this working on Heroku?

Upvotes: 0

Views: 15

Answers (1)

Chris
Chris

Reputation: 137073

I'm not sure why this isn't working right after you accept an image, but you shouldn't be storing uploaded images on Heroku's local filesystem anyway. That filesystem is ephemeral: any changes you make to it will be lost the next time your dyno restarts. This happens frequently (at least once per day).

Instead, Heroku recommends using something like Amazon S3 for uploads and provides a guide for doing so with Rails.

Upvotes: 3

Related Questions