igorkasyanchuk / new_ckeditor

Ruby on Rails + CKEditor 5
https://www.railsjazz.com/
MIT License
33 stars 6 forks source link

Upload an image to ActiveAdmin | Couldn't upload file: #6

Closed antnbaranov closed 2 years ago

antnbaranov commented 2 years ago

Error: Couldn't upload file: IMG_0329.JPG

Greeting. First of all, thanks for the gem! I'm trying to install it on activeadmin. Everything is fine, but an error occurs when loading the image. What should I try to check, recommendations for finding an error?

Everything is done according to the documentation

  1. gem 'new_ckeditor'
  2. bundle i
  3. rails g new_ckeditor
  4. add js in application.js (require 'new_ckeditor/classic/ckeditor')
  5. form: = f.ckeditor :body

It works fine, we add image loading

  1. Let's start with new controller rails g controller file_uploads upload
    class FileUploadsController < ApplicationController
    # note
    # that method must return json with URL to an uploaded image
    # or error message
    def upload
    image = CkEditorImage.new(file: params["upload"])
    if image.save
      render json: {
        url: image.file.url
      }
    else
      render json: {
        "error": {
          "message": image.errors.full_messages.join(", ")
        }
      }
    end
    end
    end

    Create a new model where images will be stored:rails g model CkEditorImage file:string user_id:integer parent_id:integer parent_type:string

class CkEditorImage < ApplicationRecord
  mount_uploader :file, CkEditorImageUploader
  belongs_to :user, optional: true
  validates_presence_of :file
end

Code of migration may look like:

class CreateCkEditorImages < ActiveRecord::Migration[6.0]
  def change
    create_table :ck_editor_images do |t|
      t.string :file
      t.integer :user_id, index: true
      t.integer :parent_id
      t.string :parent_type

      t.datetime :created_at
      t.datetime :updated_at
    end
    add_index :ck_editor_images, :user_id
    add_index :ck_editor_images, [:parent_id, :parent_type]
  end
end

Run this migration.

Create Carrierwave uploader rails g uploader CkEditorImage. Open and edit it if needed.

igorkasyanchuk commented 2 years ago

@HarshBarash I suggest debugging code on your side, I won't be able to do any investigation here. Also from your description is not clear where the error happens? On backend? Frontent? Show some logs.

antnbaranov commented 2 years ago

@igorkasyanchuk Heh, actually I wrote this issue more to help other developers, I'm a junior dev, and I thought it should be like this to help with unexpected bugs. Although the logs almost immediately hint at this when starting from scratch;) For a couple of minutes I really thought I had missed something

In the manual you you can add routes.rb: post "/upload" => "file_uploads#upload"

igorkasyanchuk commented 2 years ago

Oh, I see.

In this case, I suggest maybe making a PR improving the documentation if you want.

antnbaranov commented 2 years ago

I'm happy to, but I'm not sure that it's suitable for all cases. As soon as I check not only my task, but also the basic case, I will make changes. Thanks