Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).on 'ready', ->
- Dropzone.autoDiscover = false;
- $("#new_attachment").dropzone(
- maxFilesize: 15
- addRemoveLinks: true
- paramName: "attachment[doc]"
- success: (file, response) ->
- response = JSON.parse(file.xhr.response)
- showDocTemplate = HandlebarsTemplates['show_doc_links'](attachments: [response.attachment])
- $('.show-doc__message').replaceWith('<div class="show-doc__link-group"></div>')
- $('.show-doc__link-group').append(showDocTemplate)
- )
- = form_for [@shipment, Attachment.new], html: { multipart: true, class: 'dropzone' } do |f|
- .fallback
- = f.file_field :doc
- class AttachmentsController < ApplicationController
- before_action :set_attachment, only: :create
- load_and_authorize_resource
- respond_to :js, except: :create
- respond_to :json, only: :create
- def show
- send_file @attachment.doc.path, disposition: 'inline'
- end
- def create
- @attachment.save
- respond_with @attachment
- end
- def destroy
- respond_with @attachment.destroy, location: location
- end
- private
- def location
- if @attachment.attachable.is_a?(Flight)
- admin_flights_url(@attachment.attachable)
- else
- @attachment.attachable
- end
- end
- def attachment_params
- params.require(:attachment).permit(:doc, :doc_type)
- end
- def set_attachment
- attachable_id = params.keys.detect { |k| k =~ /(shipment|flight)_id/ }
- attachable = $1.classify.constantize.find(params[attachable_id])
- @attachment = attachable.attachments.build(attachment_params)
- end
- end
Add Comment
Please, Sign In to add comment