#Use tinymce-rails & tinymce-rails-imageupload with carrierwave & fog
Follow the files and put them in their appropriate directories. Should work for you!
# 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 |
# 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 |