File: README.rdoc

package info (click to toggle)
ruby-remotipart 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: ruby: 136; javascript: 136; makefile: 4
file content (142 lines) | stat: -rw-r--r-- 5,379 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
= Remotipart: Rails jQuery File Upload

Remotipart is a Ruby on Rails gem enabling AJAX file uploads with jQuery in Rails 3 and Rails 4 remote forms.
This gem augments the native Rails jQuery remote form functionality enabling asynchronous file uploads with little to no modification to your application.

* {Homepage and Demos}[http://www.alfajango.com/blog/remotipart-rails-gem/]
* {How AJAX File Uploads Work}[http://www.alfajango.com/blog/ajax-file-uploads-with-the-iframe-method/]

== Dependencies

* {Rails 3 or Rails 4}[http://github.com/rails/rails]
* {The jquery-rails gem}[http://rubygems.org/gems/jquery-rails] (included in Rails 3 and Rails 4 by default), which installs {jQuery}[http://jquery.com] and the {Rails jQuery driver (jquery-ujs)}[https://github.com/rails/jquery-ujs]

== Installation

<b>Your app should be using jquery-rails gem v2.3.0 or above.</b>

If you're using an old version of the jquery-rails gem,
make sure you have a supported jquery-ujs (rails.js or jquery_ujs.js) version from {VERSION_COMPATIBILITY}[https://github.com/JangoSteve/remotipart/blob/master/VERSION_COMPATIBILITY.rdoc].

[1.]
  Install the Remotipart gem

  * Add this line to your GEMFILE (use the appropriate version from the compatibilty chart if needed)

   gem 'remotipart', '~> 1.2'

  * And run

   bundle install

=== Rails 3.1

[2.]
  The necessary js files will automatically be added to the asset pipeline, so add the following to app/assets/javascripts/application.js (right after <tt>//= require jquery_ujs</tt>):

   //= require jquery.remotipart

=== Rails 3.0

[2.]
  Run the Remotipart install generator to add jquery.iframe-transport.js and jquery.remotipart.js to public/javascripts/

   rails g remotipart:install

[3.]
  The necessary js files will be added to your app's javascript <tt>:defaults</tt>, so make sure the following is in your application layout:

   <%= javascript_include_tag :defaults %>

== 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') )
      });

=== 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 %>

== Note on Patches/Pull Requests

<b>If you have a general improvement, optimization, or refactoring, please {read this first}[https://github.com/formasfunction/remotipart/wiki/Refactoring-and-Improving-Remotipart].</b>

* 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.

== Tests

Because of the nature of AJAX file uploads and certain browser restrictions, we could not simply create unit tests (using qunit or jasmine)
to test the file upload functionality of remotipart (since the browsers running those test suites won't allow us to set the target of a file
upload input using javascript). So, instead we created a demo Rails app using remotipart with all remotipart functionality tested using RSpec and Capybara.

* {Demo Rails App with Tests}[https://github.com/JangoSteve/Rails-jQuery-Demo/tree/remotipart]
  * {Tests}[https://github.com/JangoSteve/Rails-jQuery-Demo/blob/remotipart/spec/integration/comments_spec.rb]

To run tests:

Clone the remotipart branch of the demo app
  git clone -b remotipart git://github.com/JangoSteve/Rails-jQuery-Demo.git

Install the dependencies
  bundle install

Run the tests
  bundle exec rspec spec/

If you need to test your own changes to remotipart, just update the Gemfile with your own fork/branch of remotipart:

  gem 'remotipart', :git => 'git://github.com/MY_FORK/remotipart.git', :branch => 'MY_BRANCH'

== Special Thanks

Thank you to Greg Leppert for writing the original version of this gem and providing inspiration for the gem in its current incarnation.

Thank you to {Adam Kerr}[https://github.com/ajrkerr] for helping move over to the simpler jQuery 1.6-compatible iframe-transport.js and for helping write the rack middleware, making remotipart even easier to use in Rails.

== Copyright

Copyright (c) 2013 {Steve Schwartz}[https://github.com/JangoSteve], {Greg Leppert}[https://github.com/leppert]. See LICENSE for details.