Skip to content

Instantly share code, notes, and snippets.

@dannyvassallo
Created February 16, 2016 21:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dannyvassallo/f0fff53bfd88114ec4e7 to your computer and use it in GitHub Desktop.
Save dannyvassallo/f0fff53bfd88114ec4e7 to your computer and use it in GitHub Desktop.
Use tinymce-rails & tinymce-rails-imageupload with carrierwave & fog
# db/migrate/filename.rb
# generate with >> rails g model image alt:string hint:string file:string
class CreateImages < ActiveRecord::Migration[5.0]
def change
create_table :images do |t|
t.string :alt
t.string :hint
t.string :file
t.timestamps
end
end
end

#Use tinymce-rails & tinymce-rails-imageupload with carrierwave & fog

Follow the files and put them in their appropriate directories. Should work for you!

# config/initializers/fog.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
config.fog_directory = ENV['AWS_BUCKET']
config.fog_public = true
end
# Gemfile
...
# carrierwave
gem 'carrierwave'
gem 'mini_magick'
gem 'fog'
# wysiwyg
gem 'tinymce-rails'
gem 'tinymce-rails-imageupload', github: 'PerfectlyNormal/tinymce-rails-imageupload'
...
# app/models/image.rb
class Image < ApplicationRecord
mount_uploader :file, ImageUploader
end
# app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
# app/controllers/images_controller.rb
class ImagesController < ApplicationController
def new
@image = Image.build.params(image_params)
end
def show
@image = Image.find(params[:id])
end
private
def image_params
params.require(:image).permit(
:file,
:hint,
:alt,
)
end
end
# config/tinymce.yml
toolbar:
- styleselect | bold italic | undo redo
- uploadimage | link
plugins:
- uploadimage
- link
# app/controllers/tinymce_assets_controller.rb
class TinymceAssetsController < ApplicationController
def create
# Take upload from params[:file] and store it somehow...
# Optionally also accept params[:hint] and consume if needed
image = Image.create params.permit(:file, :alt, :hint)
render json: {
image: {
url: image.file.url
}
}, content_type: "text/html"
end
end
Copy link

ghost commented Jun 8, 2017

I get a ActionController::RoutingError (No route matches [POST] "/tinymce_assets"):`

error when trying to implement this, both locally and on Heroku.

@kbs4674
Copy link

kbs4674 commented Jun 27, 2017

How about do this?

(previous step +)

/config/routes.rb

post '/tinymce_assets' => 'tinymce_assets#create'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment