https://github.com/jbox-web/remotipart
https://github.com/jbox-web/remotipart
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jbox-web/remotipart
- Owner: jbox-web
- License: mit
- Created: 2019-02-04T01:05:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-06T16:07:14.000Z (10 months ago)
- Last Synced: 2024-09-06T19:14:44.657Z (10 months ago)
- Language: Ruby
- Size: 290 KB
- Stars: 2
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Remotipart: Rails jQuery File Uploads
[](https://github.com/jbox-web/remotipart/blob/master/LICENSE)
[](https://github.com/jbox-web/remotipart/releases/latest)
[](https://github.com/jbox-web/remotipart/actions)
[](https://codeclimate.com/github/jbox-web/remotipart)
[](https://codeclimate.com/github/jbox-web/remotipart/coverage)Remotipart is a Ruby on Rails gem enabling AJAX file uploads with jQuery in Rails 5 remote forms.
This gem augments the native Rails jQuery remote form functionality enabling asynchronous file uploads with little to no modification to your application.
* [How AJAX File Uploads Work](http://www.alfajango.com/blog/ajax-file-uploads-with-the-iframe-method/)
## Installation
Put this in your `Gemfile` :
```ruby
git_source(:github){ |repo_name| "https://github.com/#{repo_name}.git" }gem 'remotipart', github: 'jbox-web/remotipart', tag: '1.6.0'
```then run `bundle install`.
## Usage
* For multipart / forms with file inputs, set your form_for to remote as you would for a normal ajax form : `remote: true`
* When Javascript is enabled in the user's browser, the form, including the file, will be submitted asynchronously to your controller with : `format: 'js'`If you need to determine if a particular request was made via a remotipart-enabled form :
* from your Rails controller or view :
```ruby
if remotipart_submitted?
```* from your javascript :
```javascript
$(form).bind("ajax:success", function(){
if ($(this).data('remotipartSubmitted')) {
...
}
});
```If you want to be notified when the upload is complete (which can be either success or error)
* from your javascript :
```javascript
$(form).on("ajax:remotipartComplete", function(e, data){
console.log(e, data)
});
```## Example
`sample_layout.html.erb` :
```html
<%= form_for @sample, html: { multipart: true }, remote: true do |f| %>
<%= f.label :file %>
<%= f.file_field :file %>
<%= f.submit %>
<% end %>
````sample_controller.rb` :
```ruby
def create
respond_to do |format|
if @sample.save
format.js
end
end
end
````create.js.erb` :
```javascript
// Display a Javascript alert
alert('success!');
<% if remotipart_submitted? %>
alert('submitted via remotipart')
<% else %>
alert('submitted via native jquery-ujs')
<% end %>
```The content type requested from the application can be overridden via the data-type HTML5 attribute:
`sample_layout2.html.erb` :
```html
<%= form_for @sample, html: { multipart: true }, remote: true, data: { type: :html } do |f| %>
<%= f.label :file %>
<%= f.file_field :file %>
<%= f.submit %>
<% end %>
```In this case, the application should serve HTML using a create.html.erb template instead of JavaScript.