Method: FileAttachmentsController#create

Defined in:
app/controllers/file_attachments_controller.rb

#createObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/file_attachments_controller.rb', line 52

def create
  if params[:file]
    file_params = {
      :uploaded_file => params[:file]
    }
    if params[:attachable_id] && params[:attachable_type]
      file_params.merge!({
        :attachable_id   => params[:attachable_id],
        :attachable_type => params[:attachable_type]
      })
    end
    @file_attachment = FileAttachment.new file_params
  elsif params[:file_attachment] && params[:file_attachment][:uploaded_file]
    @file_attachment = FileAttachment.new params[:file_attachment]
  end
  
  if @file_attachment && @file_attachment.save
    flash[:notice] = "File uploaded."
  else
    if @file_attachment
      flash[:warning] = "Unable to save file attachment: #{@file_attachment.errors.full_messages.join('; ')}"
    else
      flash[:warning] = "No files were selected for upload."
    end
  end
  unless params[:file] # request.xhr? # html5 based multiple uploads are not xhr ?
    redirect_to_index_or_attachable(:std => 1) # {:std => 1} - make sure the std html form displays
  else
    flash.discard
    render :partial => 'file_attachments/file_attachment', :object => @file_attachment
  end
end