0

I am seeing something odd in my Rails app, which is using a gem / blog engine called Line for blog posts. In those blog posts, you can upload images in line, and those images are stored in a table LinesPicture.

Everything is fine locally, and it works when I push live to Heroku. In other words, I can write three blogs with pictures in each blog post, those images are stored in LinesPicture table, and they load normally.

Then, if I push anything from local branch to Heroku thereafter, the images no longer show in the post and the error [Alt Text] comes instead.

Checking the source of the post, it is still linked to the internal image file:

<div class="article_content">    
        <p><p><img src="/uploads/lines/picture/image/5/aac_divisions.png" alt="Alt Text"/></p>

I assumed this meant the image was no longer in the table, but if I run heroku run rails console and then LinesPicture.last, the image is definitely still there:

LinesPicture.last
  LinesPicture Load (1.2ms)  SELECT  "lines_pictures".* FROM "lines_pictures"  ORDER BY "lines_pictures"."id" DESC LIMIT 1
=> #<LinesPicture id: 5, image: "aac_divisions.png", name: "Aac Divisions", article_id: 1, created_at: "2017-08-19 21:15:42", updated_at: "2017-08-19 21:15:48">

So in short, the image is loading, subsequent pushes are causing an issue, and yet the image file ("aac_divisions.png") is still stored in the table.

Last step, I tried running heroku logs but didn't see anything useful. Concludes with:

 request_id=5d89a754-429a-46f8-9238-52f1108849ed fwd="98.234.48.100" dyno=web.1 connect=1ms service=20ms status=404 bytes=1829 protocol=https
2017-08-19T21:38:08.487398+00:00 heroku[router]: at=info method=GET path="/uploads/lines/picture/image/5/aac_divisions.png" host=quiet-coast-71929.herokuapp.com request_id=a6af52fa-a86d-4008-a924-6e68a307b9af fwd="98.234.48.100" dyno=web.1 connect=0ms service=22ms status=404 bytes=1829 protocol=https
2017-08-19T21:41:52.151184+00:00 heroku[run.8811]: State changed from up to complete
2017-08-19T21:41:52.138358+00:00 heroku[run.8811]: Process exited with status 0

Any idea why this might happen?

darkginger
  • 652
  • 1
  • 10
  • 38

1 Answers1

0

In production.rb change

config.assets.compile = false

to

config.assets.compile = true

If you are using Rails 4 or an even earlier version Rails, you must add gem 'rails_12factor', group: :production into your Gemfile in order for Heroku to serve your static assets.

If you are using Rails 5 this gem is not needed. From Heroku:

Previous versions of Rails required you to add a gem to your project rails_12factor to enable static asset serving and logging on Heroku. If you are deploying a new application this gem is not needed. If you are upgrading an existing application you can remove this gem provided you have the appropriate configuration in your config/environments/production.rb file

Finally push to heroku again.

Belder
  • 768
  • 1
  • 10
  • 17
  • Made the change but generated a new error: `RAILS_ENV=production rake assets:precompile rake aborted! No such middleware to insert before: ActionDispatch::Static`. Working through why the rake is filing now. Will report back if I figure it out. – darkginger Aug 20 '17 at 01:43
  • hmm.. see what happens when you change `config.serve_static_assets = false ` to `config.serve_static_assets = true` in your production.rb. – Belder Aug 20 '17 at 02:04
  • Yes, was trying that after I saw the error. Seems to cause: `ExecJS::RuntimeError: (execjs):1`, and I am reading through some Stack Overflow questions on why that would happen. – darkginger Aug 20 '17 at 02:09
  • Update: Followed the second answer here. Installed two gems, and now it runs. https://stackoverflow.com/questions/17051358/rails-execjs-runtime-error – darkginger Aug 20 '17 at 02:16
  • Last Update: Good news, it was able to run after installing those gems. Unfortunately after pushing to Heroku, the problem still persists. Must be something else. Thank you though. – darkginger Aug 20 '17 at 02:23
  • Ok, got it. The trick is that you also need to add `gem 'rails_12factor', group: :production` into your gemfile, per Heroku's documentation. Seems to be working now. Can you update your answer, and I'll accept. – darkginger Aug 20 '17 at 02:42
  • Ah, yes... you're right. I forgot to mention the 12factor gem. Are you using Rails 5? I believe that it's no longer necessary to install the gem anymore with 5 (I think). Otherwise Heroku requires it. – Belder Aug 20 '17 at 09:44