Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luciuschoi/e242423853c458bbb3f73da5bc7cc240 to your computer and use it in GitHub Desktop.
Save luciuschoi/e242423853c458bbb3f73da5bc7cc240 to your computer and use it in GitHub Desktop.
summernote image upload flow source
== Coffe File
ready = ->
sendFile = (file) ->
data = new FormData
data.append 'content_image[image]', file
$.ajax
data: data
type: 'POST'
url: '/api/uploads'
cache: false
contentType: false
processData: false
success: (data) ->
$(".summernote").summernote "insertImage", data.url
deleteFile = (file_url) ->
$.ajax
del_data = new FormData
del_data.append 'content_image[url]', file_url
$.ajax
data: del_data
type: 'DELETE'
url: '/api/removes'
cache: false
contentType: false
processData: false
success: (data) ->
if data.status is false
alert "Delete Fail"
else
true
$('.summernote').summernote
callbacks:
onImageUpload: (files) ->
sendFile files[0]
onMediaDelete: ($target, editor, $editable) ->
deleteFile $target[0].src
$(document).ready(ready)
$(document).on('page:load', ready)
== Controller
class Api::ContentImagesController < ApplicationController
def create
@content_image = ContentImage.new(image_params)
@content_image.save
respond_to do |format|
format.json { render :json => { url: @content_image.image.url } }
end.
end
def destroy
#id = del_image_params[:url].split("/").last(2).first # FOR FIND ID
image_name = del_image_params[:url].split("/").last # FOR Find IMAGE_NAME.
@content_image = ContentImage.where(image: image_name).first
if @content_image.present?
@content_image.destroy
respond_to do |format|.
format.json { render :json => { status: true } }
end
else
respond_to do |format|.
format.json { render :json => { status: false} }
end
end.
end
private
def image_params
params.require(:content_image).permit(:image)
end
def del_image_params
params.require(:content_image).permit(:url)
end
end
== Routing File
Rails.application.routes.draw do
namespace(:api){
post 'uploads' => 'content_images#create'
delete 'removes' => 'content_images#destroy'
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment