Class: PaperclipDatabaseStorage::AttachmentsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/paperclip_database_storage/attachments_controller.rb

Instance Method Summary collapse

Instance Method Details

#get_attachmentObject

Raises:

  • (ActionController::RoutingError)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/paperclip_database_storage/attachments_controller.rb', line 2

def get_attachment
  conditions = {}
  conditions[:attached_type] = params[:class].singularize.camelize if params[:class]
  conditions[:attached_id] = params[:id] if params[:id]
  conditions[:attached_id] ||= params[:id_partition].gsub(/\//, '').to_i if params[:id_partition]
  conditions[:attachment_name] = params[:attachment].singularize if params[:attachment]
  conditions[:style] = params[:style] if params[:style]
  

  attachments = PaperclipDatabaseStorage::Attachment.where(conditions)

  raise ActionController::RoutingError.new('Image not Found') if attachments.empty?
  raise ActionController::RoutingError.new('Too many images found. Check your route definition') if attachments.length > 1

  attachment = attachments.first
  send_data attachment.file_data, :type => attachment.content_type
end