Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
NOTE! THIS REPO HAS BEEN MOVED. SEE THE README. Remotipart is a Ruby on Rails gem enabling remote multipart forms (AJAX style file uploads) with jQuery.
Ruby
tag: v0.3.4

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.document
.gitignore
History.rdoc
LICENSE
README.rdoc
Rakefile
VERSION
remotipart.gemspec

README.rdoc

Remotipart

Remotipart is a Ruby on Rails gem enabling remote multipart forms (AJAX style file uploads) with jQuery. This gem augments the native Rails jQuery remote form function enabling asynchronous file uploads with little to no modification to your application.

Dependencies

Version Compatibility

+---------------------------------------------------------------------+
| Version of remotipart.js | is compatible with versions of rails.js* |
+---------------------------------------------------------------------+
|        <= 0.3.1          |                      <= 498b35e24cdb14f2 |
|           0.3.2          |  > 498b35e24cdb14f2, <= 7f2acc1811f62877 |
|           0.3.3          |  > 7f2acc1811f62877, <= d2abd6f9df4e4a42 |
|           0.3.4          |  > d2abd6f9df4e4a42 (i.e. latest)        |
+---------------------------------------------------------------------+

*rails.js versions (For a full list of Rails.js]versions and diffs, see Rails jQuery UJS Changelog):

Installation

Prerequisite: Make sure you have a supported jquery-ujs (rails.js) version from the previous section.

  1. Install the Remotipart gem

    • Add this line to your GEMFILE (add the correct version from previous section if needed)

    gem 'remotipart'
    • And run

    bundle install
  2. Run the Remotipart generator to add jquery.remotipart.js to public/javascripts/

rails g remotipart

  1. Add the Javascript files for jQuery, the Rails jQuery driver, jQuery Form plugin, and Remotipart to your template, making sure to include jquery.remotipart.js after jQuery and the Rails jQuery driver

<%= javascript_include_tag 'jquery-1.4.4.min', 'rails', 'jquery.form', 'jquery.remotipart' %>

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'

  • In the JS response template for your controller action, wrap all of your response code in one remotipart_response block:

<%= remotipart_response do %> All Javascript response code goes here <% end %>

  • The options available to the text_area_tag can be passed to remotipart_response for further control over the response. For instance:

<%= remotipart_response({:escape => false}) do %> All Javascript response code goes here <% end %>

  • UPDATE: As of v0.3.0, anything inside the remotipart_response block will be rendered normally, unless the request actually did come from a remotipart-submitted form. So js.erb responses can be shared between remotipart and non-remotipart forms.

    <%= remotipart_response do %>
      if params[:remotipart_submitted]
        // do one thing
      else
        // do another
      end
    <% end %>
  • If you need to determine if a particular request was made via a remotipart-enabled form…

    • from your Rails controller or view:

      if params[:remotipart_submitted]
    • from your javascript:

      $(form).bind("ajax:success", function(){
        if ( $(this).data('remotipartSubmitted') )
      });

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

<%= remotipart_response do %>
  //Display a Javascript alert
  alert('success!');
<% end %>

Note on Patches/Pull Requests

If you have a general improvement, optimization, or refactoring, please read this first.

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2011 Greg Leppert. See LICENSE for details.

Something went wrong with that request. Please try again.