Ask A Question

Notifications

You’re not receiving notifications from this thread.

Image Attachments in ActionText not showing due to routing

Benjamin Deutscher asked in Rails

Hi,
I am desperatly trying to get my ActionText site working in production. I have a it working in development. In production the upload and editing is working fine, I get the images saved on local disk. But when showing the page I only get a broken image. Visiting the image url will result in an error 404.

The URL is something like example.com/rails/active_storage/representations/SIGNED_ID/myimage.jpeg.
Using rails c I get all attachments of the rich_text body. Is there a route I need to configure to get the images to show?

Edit
Some more information

  • I am running unicorn and nginx
  • ruby 2.6.5p114
  • rails 6.0.2.1

config/storage.yml

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

production.rb

 # Store uploaded files on the local file system (see config/storage.yml for options).
  config.active_storage.service = :local

routes.rb

Rails.application.routes.draw do
  resources :pages, param: :seo_url, path: 'seite'

  get '/index', to: 'application#home', as: :home
  root to: 'application#home'
end

Regards,
Ben

Reply

Looks like it's using example.com for the domain so you're probably missing one of the configs to set the default host.

Lots of chat about it in this thread. https://github.com/rails/rails/issues/32500#issuecomment-441664690

And one of the recommended options to try is:

#config/environments/production.rb
config.active_storage.service = :local
config.action_mailer.default_url_options = { host: 'https://domain.com' }
config.default_url_options = { host: "https://domain.com" }
Reply

Hi Chris,
thanks for your response! I might haven't made my problem clear enough. I do get the correct link to the image with my real domain. URL wise everything looks fine so far. It is just the link itself that is not working. Sorry for the inconvenience!

This is the output that is produced. MYDOMAIN is my actual domain.

<div class="trix-content">
  <div><action-text-attachment sgid="BAh7CEkiCGdpZAY6BkVUSSI1Z2lkOi8va3Voc2VpdGUvQWN0aXZlU3RvcmFnZTo6QmxvYi8zOT9leHBpcmVzX2luBjsAVEkiDHB1cnBvc2UGOwBUSSIPYXR0YWNoYWJsZQY7AFRJIg9leHBpcmVzX2F0BjsAVDA=--bafaf208e014e92662a15626e443eeeef2d677f0" content-type="image/png" url="http://MYDOMAIN/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBMQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--cd93111a807a97c12f5fb5d0fe1447d015ea0766/TryAndError.png" filename="TryAndError.png" filesize="1178020" width="1920" height="1080" previewable="true" presentation="gallery"><figure class="attachment attachment--preview attachment--png">
    <img src="http://MYDOMAIN/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBMQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--cd93111a807a97c12f5fb5d0fe1447d015ea0766/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9VY21WemFYcGxYM1J2WDJ4cGJXbDBXd2RwQWdBRWFRSUFBdz09IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--1bd348c8e1d1c843a4eda82746c8ac2d4f33faae/TryAndError.png">

  <figcaption class="attachment__caption">
      <span class="attachment__name">TryAndError.png</span>
      <span class="attachment__size">1.12 MB</span>
  </figcaption>
</figure></action-text-attachment></div>
</div>

Using the link from the src attribute of the img tag http://MYDOMAIN/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBMQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--cd93111a807a97c12f5fb5d0fe1447d015ea0766/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9VY21WemFYcGxYM1J2WDJ4cGJXbDBXd2RwQWdBRWFRSUFBdz09IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--1bd348c8e1d1c843a4eda82746c8ac2d4f33faae/TryAndError.png takes me to my 404 error page.

Reply

Hi,
I was able to solve my problem. I misconfigured my nginx. I pasted the wrong config snippet for static image delivery into the config. This is actually so embarrassing!

Thank you very much for your help.

Ben

Reply

Could you please explain how you configured nginx because I am having the same problem.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.