hekmat
hekmat

Reputation: 103

Images breaking in production in Rails

I have deployed my app to heroku using Cloud9 after I changed my database from sqlite3 to postgres. my app hasn't been behaving well after that. For example images in production break after two or three hour from uploading, it is bad. I also have to start the PostgreSQL every couple hour in order to be able to use development sudo service postgresql start.

This is my database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: <%= ENV['USERNAME'] %>
  password: <%= ENV['PASSWORD'] %>
  host:     <%= ENV['IP'] %>

development:
  <<: *default
  database: sample_app_development

test:
  <<: *default
  database: sample_app_test

production:
  <<: *default
  database: sample_app_production

I also heard that it has to do with production.rb, so here is mine:

    Rails.application.configure do

  config.cache_classes = true

  config.eager_load = true

  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true


  config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?


  config.assets.js_compressor = :uglifier

  config.assets.compile = true


  config.assets.digest = true


  config.log_level = :debug


  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

  config.log_formatter = ::Logger::Formatter.new

  config.active_record.dump_schema_after_migration = false
end

FYI, in this app users who sign up are able to upload images to the app, so I just can't save the pictures that I want only.

Upvotes: 0

Views: 195

Answers (2)

Karan Shah
Karan Shah

Reputation: 1324

Your problem has twofold issues, the first of them being trying to install PGSql on Heroku directly.

Understand that unlike a shared server of Digital ocean or AWS, Heroku is a PaaS (Platform as a Service) and you cannot put anything on the heroku server apart from your rails app. You have to use them through add-ons.

Adding PGSql has its own add on too, it will automatically take periodic backups for you and will be much more optimised to do DB operations.

Learn more about it here: https://elements.heroku.com/addons/heroku-postgresql

Also, Heroku does not encourage storing static assets on the server as it recycles them every time you restart your server so you will have to compile your assets everytime you deploy. To make this better, upload them to either a CDN or S3.

Upvotes: 0

Sajan
Sajan

Reputation: 1923

Since you are not sure about the path of your uploaded files, I believe your problem lies there. I have not used Heroku much, but from their article:

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted. For example, this occurs any time a dyno is replaced due to application deployment and approximately once a day as part of normal dyno management.

so if your images are uploaded to file system of code, they are not visible to other process or it will be trashed after dyno is replaced because when heroku replace a dyno, It considers only files which were deployed in last deployment. and that does not have the uploaded images.

So I suggest you to use a CDN like AWS S3.

Upvotes: 2

Related Questions