From bd1b5e142c17484f6ae518ec424f66c4fec293b7 Mon Sep 17 00:00:00 2001 From: man13 <man13@inf.ufpr.br> Date: Thu, 24 Mar 2016 11:46:23 -0300 Subject: [PATCH] fixing delete action Signed-off-by: man13 <man13@inf.ufpr.br> --- app/controllers/learning_objects_controller.rb | 9 ++++++++- app/policies/learning_object_policy.rb | 4 ++++ .../learning_objects/build/_file_upload_form.html.erb | 9 ++++++++- config/routes.rb | 2 +- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/controllers/learning_objects_controller.rb b/app/controllers/learning_objects_controller.rb index 7af866f1..f64f40f7 100644 --- a/app/controllers/learning_objects_controller.rb +++ b/app/controllers/learning_objects_controller.rb @@ -6,7 +6,7 @@ class LearningObjectsController < ApplicationController before_action :authenticate_user!, except: [:index, :show] before_action :set_learning_object, only: [:show, :edit, :update, - :destroy, :like, :bookmarks, + :destroy, :destroy_attachment, :like, :bookmarks, :collections, :upload, :upload_link, :download, :user_not_authorized] after_action :increment_learning_object_views, only: [:show] @@ -81,6 +81,13 @@ class LearningObjectsController < ApplicationController end end + def destroy_attachment + attachment = LearningObject::Attachment.find(params[:attachment_id]) + @learning_object.update(attachment: nil) if @learning_object.attachment == attachment + attachment.destroy + render :nothing => true, :status => 204 + end + def download @learning_object.download current_user diff --git a/app/policies/learning_object_policy.rb b/app/policies/learning_object_policy.rb index e91c7db7..5d413e3c 100644 --- a/app/policies/learning_object_policy.rb +++ b/app/policies/learning_object_policy.rb @@ -26,6 +26,10 @@ class LearningObjectPolicy < ApplicationPolicy record if owns? end + def destroy_attachment? + record if owns? + end + def show? if user.nil? record.state == 'published' diff --git a/app/views/learning_objects/build/_file_upload_form.html.erb b/app/views/learning_objects/build/_file_upload_form.html.erb index 1da81f4f..2c9c983e 100644 --- a/app/views/learning_objects/build/_file_upload_form.html.erb +++ b/app/views/learning_objects/build/_file_upload_form.html.erb @@ -30,7 +30,7 @@ <tr> <td> <div class="fileName"><%= "#{attachment.name} (#{attachment.mime_type})" %></div> - <div class="deleteFile"><i class="fa fa-trash fa-1"></i></div> + <div class="deleteFile" onclick="removeFile(<%= attachment.id %>, <%= @learning_object.id %>)"><i class="fa fa-trash fa-1"></i></div> </td> </tr> <% end %> @@ -154,6 +154,13 @@ </style> <script type="text/javascript" charset="utf-8"> + function removeFile(file_id, learning_object_id){ + $.ajax({ + url: '/learning_objects/'+learning_object_id+'/remove_file/'+file_id, + type: 'POST' + }) + } + (function () { var r = new Resumable({ target: "<%= chunk_path %>", diff --git a/config/routes.rb b/config/routes.rb index 3a6aa06c..97360dff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,10 +71,10 @@ Rails.application.routes.draw do end end + post 'learning_objects/:id/remove_file/:attachment_id' => 'learning_objects#destroy_attachment', as: 'remove_file' resources :learning_objects do resources :build, controller: 'learning_objects/build' get :reviews, to: 'reviews#list', as: 'reviews' - member do post :like get :collections -- GitLab