diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index ef73b44..991dce2 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -53,7 +53,19 @@ class BlogsController < ApplicationController end def upload_img - + @result = {status: false, message: '', text_id: params[:upload][:text_id] || ''} + begin + if params[:upload].present? && params[:upload][:img].present? && remotipart_submitted? + upload_info = upload_picture params[:upload][:img] + @result[:status] = true + @result[:message] = "![#{upload_info[:file_name]}](/images/#{upload_info[:real_file_name]})" + end + rescue UploadException => e + @result[:message] = e.message + end + respond_to do |format| + format.js + end end def preview @@ -68,6 +80,8 @@ class BlogsController < ApplicationController protected def upload_picture(file) - + upload_path = File.join Rails.root, 'public/images' + upload = SimpleFileupload.new upload_path:upload_path, max_size: 1024*1024*2, type: 'image' + upload_info = upload.upload file end end diff --git a/app/views/blogs/upload_img.js.haml b/app/views/blogs/upload_img.js.haml new file mode 100755 index 0000000..f7a05e3 --- /dev/null +++ b/app/views/blogs/upload_img.js.haml @@ -0,0 +1,8 @@ +- if @result[:status] + :plain + var old_val = $("##{@result[:text_id]}").val(); + $("##{@result[:text_id]}").val(old_val + "#{@result[:message]}"); + $('#upload_modal').modal('hide'); +- else + :plain + $('#error').text("#{@result[:message]}"); \ No newline at end of file