Upload images with TinyMCE and Rails 7

Viewed 345

I m trying to upload an image with tinymce and rails 7 (active storage and tinymce-rails gem)

My controller image :

class UploaderController < ApplicationController
  skip_forgery_protection
  def image
    blob = ActiveStorage::Blob.create_after_upload!(
      io: params[:file],
      filename: params[:file].original_filename,
      content_type: params[:file].content_type
    )
    
     render json: {location: url_for(blob)}, content_type:  "text / html"
  end
end

My model controller (protected section) :

class Back::ThemesController < BackController
  before_action :theme, only: [:edit, :update, :destroy]

  def new
    @theme = Theme.new
  end

  def create
    @theme = Theme.new(theme_params)
    if @theme.save
      redirect_to admin_themes_path
    else
      render :new
    end
  end

My tinymce config :

toolbar:
   - file edit view insert format tools
plugins:
   - image
images_upload_url: '/uploader/image'

My routes :

post ‘uploader/image’, to: ‘uploader#image’

And in my form :

  <%= tinymce %>
  <%= f.input :description, as: :text, input_html: { class: "tinymce" , rows: 40, cols: 120 } %>

When I try to upload an image in my back office, tinymce return a wrong back url (http://localhost:3000/uploader/image ) and I get a routing error :

Started POST "/uploader/image" for ::1 at 2021-11-14 12:53:17 +0100
Processing by UploaderController#image as */*
  Parameters: {"file"=>#<ActionDispatch::Http::UploadedFile:0x00007fd46f8d2b88 @tempfile=#<Tempfile:/var/folders/_r/ytzkgwbd15jfz9w_csgk1jn00000gn/T/RackMultipart20211114-53999-wsms1f.jpg>, @original_filename="80s.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"80s.jpg\"\r\nContent-Type: image/jpeg\r\n">}
Completed 200 OK in 2ms (Views: 0.2ms | ActiveRecord: 0.0ms | Allocations: 892)


Started GET "/uploader/image" for ::1 at 2021-11-14 12:53:17 +0100

ActionController::RoutingError (No route matches [GET] "/uploader/image"):
0 Answers
Related