Skip to content

Instantly share code, notes, and snippets.

@danjellesma
Last active May 13, 2019 03:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save danjellesma/15376f0cd00f64da040b to your computer and use it in GitHub Desktop.
Save danjellesma/15376f0cd00f64da040b to your computer and use it in GitHub Desktop.
Rails 4: Adding a spinner on link clicks

In Rails 4 it is useful to have a 'page loading' indicator when a user clicks a link. This is a simple way to accomplish that without external libraries. Tested on Rails 4 using TurboLinks.

Great Link for Spinning Loader gifs: http://loading.io/


On CSS Page:
.modal {
    display:    none;
    position:   fixed;
    z-index:    1000;
    top:        0;
    left:       0;
    height:     100%;
    width:      100%;
    background: rgba( 255, 255, 255, .8 )
                url(image-path('spinner.gif'))
                50% 50%
                no-repeat;
}

Javascript (Coffeescript) -- Note: Uses Turbolinks

$(document).on 'page:fetch', ->
  $('#spinner').show()
  return
$(document).on 'page:receive', ->
  $('#spinner').hide()
  return

HTML - Use on application.html.haml

.modal#spinner{style:"display:none;"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment