Last Updated: April 22, 2016
·
1.743K
· tmartin314

General Ajax Spinner for Rails

Using spinjs-rails

Add the gem to our Gemfile:

gem 'spinjs-rails'

Add the javascripts to the asset pipeline:

//= require spin
//= require jquery.spin

Create a global listener using behaviors:

# app/javascript/main.js.coffee
$ ->
  $('[data-behavior~=ajax-spin').on 'click', ->
    target = $(this).data('target')
    $(target).spin('large')

Add the behavior to the view:

# app/views/general/index.html.slim
= link_to 'My Link',  my_link_path,
                      remote: true,
                      data: { behavior: 'ajax-spin', target: '#target-element'  }

...
#target-element
  .some-content