Ask A Question

Notifications

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
production:
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;
        xhr.open("POST", endpoint, true);
        xhr.setRequestHeader("X-CSRF-Token", csrfToken);

        xhr.upload.onprogress = function(event) {
            var progress = event.loaded / event.total * 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()) : []
                image_ids.push(data.image_id);
                image_ids_input.val(JSON.stringify(image_ids));
                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

include ::ImageUploader::Attachment.new(:image)
belongs_to :post, optional:true

end
`

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 = Image.new(image_params)

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

  private

  def image_params
    params.require(:image).permit(:image)
  end
end

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

def create
    @post = Post.new(post_params)

    if @post.save
      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: @post.id)
      end
      redirect_to pages_all_posts_path
    else
      render "new"
    end
  end

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:
https://github.com/WebSepp/Blogapp

Reply
Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.