Guest User

Untitled

a guest
Oct 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. $(document).on 'ready', ->
  2. Dropzone.autoDiscover = false;
  3.  
  4. $("#new_attachment").dropzone(
  5. maxFilesize: 15
  6. addRemoveLinks: true
  7. paramName: "attachment[doc]"
  8. success: (file, response) ->
  9. response = JSON.parse(file.xhr.response)
  10. showDocTemplate = HandlebarsTemplates['show_doc_links'](attachments: [response.attachment])
  11.  
  12. $('.show-doc__message').replaceWith('<div class="show-doc__link-group"></div>')
  13. $('.show-doc__link-group').append(showDocTemplate)
  14. )
  15.  
  16. = form_for [@shipment, Attachment.new], html: { multipart: true, class: 'dropzone' } do |f|
  17. .fallback
  18. = f.file_field :doc
  19.  
  20. class AttachmentsController < ApplicationController
  21. before_action :set_attachment, only: :create
  22.  
  23. load_and_authorize_resource
  24.  
  25. respond_to :js, except: :create
  26. respond_to :json, only: :create
  27.  
  28. def show
  29. send_file @attachment.doc.path, disposition: 'inline'
  30. end
  31.  
  32. def create
  33. @attachment.save
  34. respond_with @attachment
  35. end
  36.  
  37. def destroy
  38. respond_with @attachment.destroy, location: location
  39. end
  40.  
  41. private
  42.  
  43. def location
  44. if @attachment.attachable.is_a?(Flight)
  45. admin_flights_url(@attachment.attachable)
  46. else
  47. @attachment.attachable
  48. end
  49. end
  50.  
  51. def attachment_params
  52. params.require(:attachment).permit(:doc, :doc_type)
  53. end
  54.  
  55. def set_attachment
  56. attachable_id = params.keys.detect { |k| k =~ /(shipment|flight)_id/ }
  57. attachable = $1.classify.constantize.find(params[attachable_id])
  58. @attachment = attachable.attachments.build(attachment_params)
  59. end
  60. end
Add Comment
Please, Sign In to add comment