carrierwave
https://github.com/carrierwaveuploader/carrierwave
Ruby
Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks
CarrierWave::Mount#mount_uploaders
Mounts the given uploader on the given array column. This means that
assigning and reading from the array column will upload and retrieve
multiple files. Supposing that a User class has an uploader mounted on
images, and that images can store an array, for example it could be a
PostgreSQL JSON column. You can assign and retrieve files like this:
@user.images # => []
@user.images = [some_file_object]
@user.images # => [<Uploader>]
@user.images[0].url # => '/some_url.png'
It is also possible (but not recommended) to omit the uploader, which
will create an anonymous uploader class.
Passing a block makes it possible to customize the uploader. This can be
convenient for brevity, but if there is any significant logic in the
uploader, you should do the right thing and have it in its own file.
=== Added instance methods
Supposing a class has used +mount_uploaders+ to mount an uploader on a column
named +images+, in that case the following methods will be added to the class:
[images] Returns an array of uploaders for each uploaded file
[images=] Caches the given files
[images_urls] Returns the urls to the uploaded files
[images_cache] Returns a string that identifies the cache location of the files
[images_cache=] Retrieves the files from the cache based on the given cache name
[remote_image_urls] Returns previously cached remote urls
[remote_image_urls=] Retrieve files from the given remote urls
[remove_images] An attribute reader that can be used with a checkbox to mark the files for removal
[remove_images=] An attribute writer that can be used with a checkbox to mark the files for removal
[remove_images?] Whether the files should be removed when store_image! is called.
[store_images!] Stores all files that have been assigned with +images=+
[remove_images!] Removes the uploaded file from the filesystem.
[images_integrity_error] Returns an error object if the last files to be assigned caused an integrity error
[images_processing_error] Returns an error object if the last files to be assigned caused a processing error
[images_download_error] Returns an error object if the last files to be remotely assigned caused a download error
[image_identifiers] Reads out the identifiers of the files
=== Parameters
[column (Symbol)] the attribute to mount this uploader on
[uploader (CarrierWave::Uploader)] the uploader class to mount
[options (Hash{Symbol => Object})] a set of options
[&block (Proc)] customize anonymous uploaders
=== Options
[:mount_on => Symbol] if the name of the column to be serialized to differs you can override it using this option
[:ignore_integrity_errors => Boolean] if set to true, integrity errors will result in caching failing silently
[:ignore_processing_errors => Boolean] if set to true, processing errors will result in caching failing silently
=== Examples
Mounting uploaders on different columns.
class Song
mount_uploaders :lyrics, LyricsUploader
mount_uploaders :alternative_lyrics, LyricsUploader
mount_uploaders :files, SongUploader
end
This will add an anonymous uploader with only the default settings:
class Data
mount_uploaders :csv_files
end
this will add an anonymous uploader overriding the store_dir:
class Product
mount_uploaders :blueprints do
def store_dir
'blueprints'
end
end
endEdit
git clone [email protected]:carrierwaveuploader/carrierwave.git
cd carrierwave
open lib/carrierwave/mount.rb
Contribute
# Make a new branchgit checkout -b -your-name--update-docs-CarrierWave--Mount-mount_uploaders-for-pr
# Commit to gitgit add lib/carrierwave/mount.rbgit commit -m "better docs for CarrierWave::Mount#mount_uploaders"
# Open pull requestgem install hub # on a mac you can `brew install hub`
hub fork
git push <your name> -your-name--update-docs-CarrierWave--Mount-mount_uploaders-for-pr
hub pull-request
# Celebrate!