Last active
May 24, 2016 14:09
-
-
Save ggomagundan/70e696d8f3c6de904843e04d050c0b00 to your computer and use it in GitHub Desktop.
summernote image upload flow source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
== 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