pretty_file_input
pretty_file_input is an attempt to standardize the always-problematic file input in our Rails apps.
Benefits
- Immediate uploads via AJAX for
persisted?
records - Upload percentage displayed while uploading
- Identical user interface (without AJAX uploads) for non-
persisted?
records - Use your existing models and controllers, no changes necessary
Requirements
- carrierwave
- simple_form (if using the "Automatic" implementation below)
- jQuery
- jquery.form
Installation
# Gemfile
gem 'pretty_file_input'
# application.css
*= require pretty_file_input
# application.js
//= require pretty_file_input
Implementation
There are a couple of ways to use this gem:
Automatic
By taking advantage of the included simple_form input class, you can start using pretty_file_input with as little as one line of code:
f.input :avatar, as: :pretty_file_input
The resulting behavior will depend on whether or not the parent object is already persisted in the database.
For existing records, pretty_file_input will upload and remove files immediately, displaying some nice UI feedback along the way:
For new records, pretty_file_input will not perform any AJAX requests. (Carrierwave stores files alongside your database records, so it's impossible to upload a file before its associated record has been created.)
Manual
If your use case doesn't fit the patterns above, you can use the "Manual" integration with pretty_file_input. PrettyFileInput::Views::Component
is a Fortitude widget that can be customized with the following options:
:name, # input name that will be sent to the server
persisted: false, # is parent object is not persisted, no AJAX calls will be made
filename: nil, # pre-populate the filename span
method: nil, # override the parent form's method
action: nil, # override the parent form's action
additional_params: {} # additional parameters to be sent to server with each request
Events
pfi:uploaded
pfi:removed