Skip to content

Instantly share code, notes, and snippets.

@bigs
Last active October 10, 2017 17:55
Show Gist options
  • Save bigs/4956380 to your computer and use it in GitHub Desktop.
Save bigs/4956380 to your computer and use it in GitHub Desktop.
Uploading files to Rackspace in a Rails app using Paperclip + Fog
# config/initializers/paperclip.rb
#######################################
RACKSPACE_CONFIG = {
'production' => {
path: '',
storage: :fog,
fog_credentials: {
provider: 'Rackspace',
rackspace_username: ENV['RACKSPACE_USERNAME'],
rackspace_api_key: ENV['RACKSPACE_API_KEY'],
persistent: false
},
fog_directory: 'static',
fog_public: true
},
'development' => {
path: '',
storage: :fog,
fog_credentials: {
provider: 'Rackspace',
rackspace_username: 'mydevuser',
rackspace_api_key: 'mydevkey',
persistent: false
},
fog_directory: 'static',
fog_public: true
}
}
Paperclip::Attachment.default_options.update(RACKSPACE_CONFIG[Rails.env])
# in a model
#########################
# Can easily replace the static 'my_file' with the actual uploaded
# filename using ':filename'
has_attached_file :my_file, path: "#{Rails.env}/my_model/:id/my_file.:extension"
# and in the controller
################################
def upload_file
@model = Model.find(params['id'])
not_found if @model.nil?
@model.update_attributes(params['model'])
end
# in a view/form
#############################
<%= form_for @model, url: upload_file_model_path, html: {multipart: true} do |f| %>
<%= f.label 'My File' %>
<%= f.file_field :my_file %>
<%= f.submit 'Upload' %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment