Giter Site home page Giter Site logo

remotipart's Introduction

Remotipart: Rails jQuery File Uploads

GitHub license GitHub release CI Code Climate Test 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.

Installation

Put this in your Gemfile :

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 :
if remotipart_submitted?
  • from your 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 :
  $(form).on("ajax:remotipartComplete", function(e, data){
    console.log(e, data)
  });

Example

sample_layout.html.erb :

<%= form_for @sample, html: { multipart: true }, remote: true do |f| %>
  <div class="field">
    <%= f.label :file %>
    <%= f.file_field :file %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

sample_controller.rb :

def create
  respond_to do |format|
    if @sample.save
      format.js
    end
  end
end

create.js.erb :

// 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 :

<%= form_for @sample, html: { multipart: true }, remote: true, data: { type: :html } do |f| %>
  <div class="field">
    <%= f.label :file %>
    <%= f.file_field :file %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

In this case, the application should serve HTML using a create.html.erb template instead of JavaScript.

remotipart's People

Contributors

ai avatar ajrkerr avatar amatsuda avatar amleaver avatar codegoalie avatar dandv avatar danring avatar edouard-chin avatar esbanarango avatar hermesdt avatar jangosteve avatar kdmny avatar koic avatar leppert avatar mattolson avatar mshibuya avatar mvelikov avatar n-rodriguez avatar ordinaryzelig avatar rcook avatar shwoodard avatar sktocha avatar tallama avatar vivid avatar vivrass avatar westonganger avatar yasirs avatar znz avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

remotipart's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.