This gem use https://github.com/blueimp/jQuery-File-Upload for upload files.
Preview:
In Gemfile:
gem "rails-uploader"
In routes:
mount Uploader::Engine => '/uploader'
Architecture to store uploaded files (cancan integration):
class Asset < ActiveRecord::Base
include Uploader::Asset
def uploader_create(params, request = nil)
ability = Ability.new(request.env['warden'].user)
if ability.can? :create, self
self.user = request.env['warden'].user
super
else
errors.add(:id, :access_denied)
end
end
def uploader_destroy(params, request = nil)
ability = Ability.new(request.env['warden'].user)
if ability.can? :delete, self
super
else
errors.add(:id, :access_denied)
end
end
end
class Picture < Asset
mount_uploader :data, PictureUploader
validates_integrity_of :data
validates_filesize_of :data, :maximum => 2.megabytes.to_i
end
For example user has one picture:
class User < ActiveRecord::Base
has_one :picture, :as => :assetable, :dependent => :destroy
fileuploads :picture
end
Find asset by foreign key or guid:
@user.fileupload_asset(:picture)
Javascripts:
//= require uploader/application
Stylesheets:
*= require uploader/application
<%= uploader_field_tag :article, :photo %>
or FormBuilder:
<%= form.uploader_field :photo %>
<%= f.input :picture, :as => :uploader %>
<%= f.input :picture, :as => :uploader %>
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Copyright (c) 2012 Aimbulance, released under the MIT license