Skip to content

Integration of jquery.form.js with the Rails asset pipeline to facilitate use of `ajaxSubmit()` for submitting forms via AJAX

License

Notifications You must be signed in to change notification settings

AquisTech/ajax_submit_rails

Repository files navigation

AjaxSubmitRails

Integrates jquery.form with the Rails asset pipeline.
Supports AJAX form submission. Also submits form with file field with AJAX request.

Version mapping

ajax_submit_rails version jquery.form version required jQuery version
0.2.0 (LATEST) 4.3.0 v1.7.2 or later
0.1.1 3.51.0-2014.06.20 v1.5 or later
0.1.0 3.51.0-2014.06.20 v1.5 or later

Installation

Add this line to your application's Gemfile:

gem 'ajax_submit_rails'

And then execute:

  $ bundle

Integration

Rails app with Asset Pipeline

If you're using the asset pipeline,
then you must add the following line to your app/assets/javascripts/application.js.

//= require jquery.form.min

Some people may need to add the extension to make it work

//= require jquery.form.min.js

You can also include unminified version (not recommended)

//= require jquery.form

Rails app without Asset Pipeline

Include javascript in your layout file as following.

For example: application.html.erb

<%= javascript_include_tag 'jquery.form.min' %>

application.html.haml or application.html.slim

= javascript_include_tag 'jquery.form.min'

Usage

Add class as ajax-submit (or any other class you wish to bind) to the form
<%= form_for @your_object, html: {class: 'ajax-submit'} do |f| %>
  // other form fields
  <%= f.submit %>
<% end %>
Add binding for ajaxSubmit() in application.js
$(function () {
  // your other code

  $(document).on('submit', 'form.ajax-submit', function(e) {
    e.preventDefault(); // prevent native submit
    $(this).ajaxSubmit();
  });
});
Looking for more steps???

No... That's it! Really!! You are done!!! 😄
Now your forms with ajax-submit class will get submitted by ajaxSubmit()

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/AquisTech/ajax_submit_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

About

Integration of jquery.form.js with the Rails asset pipeline to facilitate use of `ajaxSubmit()` for submitting forms via AJAX

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published