Save 36% for Black Friday! Learn more

All threads / Loosing my attached_images when cap deploy after switching my path so that my posts stay

Ask A Question


You’re not receiving notifications from this thread.

Loosing my attached_images when cap deploy after switching my path so that my posts stay

Sebastian asked in Databases

Okay one last question :)

To avoid that cap production deploy is killing my Posts from the database i added
adapter: sqlite3
database: /home/deploy/apps/Blogapp/releases/shared/db/production.sqlite3

as path into my database.yml and it works very well! But! My images, which i drag and drop into my posts arent saved with this method so after cap production deploy i got posts with not found images which is really bad.

The trix_attachments.js looks like that:

$(document).ready(function() {
    Trix.config.attachments.preview.caption = {
        name: false,
        size: false

    function uploadAttachment(attachment) {
        var csrfToken = $('meta[name="csrf-token"]').attr('content');
        var file = attachment.file;
        var form = new FormData;
        var endpoint = "/images";
        form.append("Content-Type", file.type);
        form.append("image[image]", file);

        xhr = new XMLHttpRequest;"POST", endpoint, true);
        xhr.setRequestHeader("X-CSRF-Token", csrfToken);

        xhr.upload.onprogress = function(event) {
            var progress = event.loaded / * 100;
            return attachment.setUploadProgress(progress);

        xhr.onload = function() {
            if (this.status >= 200 && this.status < 300) {
                var data = JSON.parse(this.responseText);
//getting the id for the image related to the post so it can be called later on:
                var image_ids_input = $("#post_image_ids");
                var image_ids = image_ids_input.val() ? JSON.parse(image_ids_input.val()) : []
                return attachment.setAttributes({
                    url: data.url,
                    href: data.url

        return xhr.send(form);

    document.addEventListener("trix-attachment-add", function(event) {
        var attachment = event.attachment;
        if (attachment.file) {
            return uploadAttachment(attachment);
my image.rb model looks like this

`class Image < ApplicationRecord

belongs_to :post, optional:true


my post model has an has_many:images relation

the images_controller.rb looks like this:

class ImagesController < ApplicationController
  respond_to :json

  def create
    image_params[:image].open if image_params[:image].tempfile.closed?

    @image =

    respond_to do |format|
        format.json { render json: { image_id:, url: @image.image_url }, status: :ok }
        format.json { render json: @image.errors, status: :unprocessable_entity }


  def image_params

and the part of the posts_controller.rb that seems to matter here looks like this:

def create
    @post =

      image_ids = params['post']['image_ids']
      image_ids = image_ids.present? ? JSON.parse(image_ids) : nil
      if image_ids.present?
        Image.where(id: image_ids).update_all(post_id:
      redirect_to pages_all_posts_path
      render "new"

seems like thats a very specific question ... cause the trix_attachment file was thankfully created and edited by Jacob who was really really supportitive and seems to be a genius !
if it helps that is my github:

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.