Ghostery extension blocking Google Analytics

I fought for a long time trying to get Google Analytics set up. I tried using the Google Tag Assistant Chrome extension, which was very helpful. I wasn’t getting any errors, I was just not getting any hits, even when I would load the site myself. So I finally figured out what was happening by checking the console – my Ghostery extension was blocking the Google Analytics script from sending its data.

So if you’re having errors with your Analytics data, be sure to turn off any privacy Chrome extensions that may be blocking your scripts. Hope this helps save you some time!

Automatically Scroll To Bottom of Page On Button Click Using Javascript / jQuery and Coffeescript

I have a website with multiple pages that are shown in order, so I have a ‘continue’ button on the bottom of each page.  I wanted my site to automatically scroll to the bottom to show the continue button when the user was finished with the page.  I imagine there are many ways to do this, but here’s what worked for me. I knew how to scroll to a certain element on the page:


but had trouble figuring out how to scroll to the bottom of the page. Here’s how I did it. Note the use of the animate function to make it nice and smooth.


$("html, body").animate({
  scrollTop: $(document).height() - $(window).height()


$("html, body").animate({ scrollTop: $(document).height() - $(window).height() });

It will probably work just using 'body' without 'html', but might depend on different browsers. I left it in just to be safe.

Credit here.

Want to learn more about Ruby on Rails?  Checkout OneMonthRails , what I used to get started in Rails. Best Rails course I’ve ever seen!

Use Bootstrap Modal as UI Alert Popup Dialog In Rails

I found a cool new gem to easily let you somewhat customize the alert dialog boxes in your rails app.  I started out using the jQuery UI dialog and attempting to customize it myself, but ran into a bit of complications.  I found the data-confirm-modal gem to be a great solution.

As the documentation says, add the gem to your gemfile and run bundle.  Then don’t forget to add it to your application.js file:

//= require data-confirm-modal

Basically it just makes all alert popup dialogs become Bootstrap modals in your rails app, such as when you delete stuff or anytime you want a confirm dialog.

Props to ifad for a simple and useful gem!

Screen Shot 2014-09-28 at 4.42.19 PM

Change or Set Form Input Value with jQuery, CoffeeScript

So this is pretty basic, but it took me 10 minutes of Googling to figure it out.  If you need to specifically assign a value to a form input, here is how to do it via coffeescript:

$(".submit").on "click", ->
  email = $('input[name="user[email]"]').val();

The key is the .val() method. To get the value of an element, you call the .val() method without passing in any parameters.

email = $('input[name="user[email]"]').val();

To set the value of an element, you pass in the value you want into the .val() method.


Some credit here.

Alert Confirm Dialog in CoffeeScript

I was trying to figure out how to create a confirmation alert dialog in coffeescript. Turns out like almost everything in coffeescript, its super easy.

$(".mybutton").on "click", ->
  if confirm "Ask a question?"
    # if answer yes
    # if answer no

credit here

CSS or Javascript Assets not Working on Heroku Production

I had a weird issue where on my local environment, everything worked fine, but on production when pushed to heroku, my javascript and css files weren’t getting loaded.

Now I don’t fully understand the asset pipeline and if/how it was involved in this error, but here’s what worked for me.

Simply manually precompile assets prior to pushing to heroku:

bundle exec rake assets:precompile

You will notice that this create a bunch of files.  Then you need to commit to git, and push to heroku again.

git add .
git commit -am "message"
git push origin master
git push heroku master

If you know a good explanation of why this is necessary, please let me know!

Credit here.

Redirect to a link with CoffeeScript

So while using CoffeeScript in my app, I wanted to redirect to another page in my rails app on a button click.  Now I didn’t want to make the button a link by itself using the “link_to” tag because I only wanted the button to redirect depending on certain criteria in Javascript (or coffee script).

As usual with CoffeeScript, its simple easy and elegant. To redirct to a link:

  $(".button").on "click", ->
    location.href = "your_rails_page"

This will redirect to

Credit here

Fix CoffeeScript Unexpected Indent Error in SublimeText

Getting this

error: unexpected INDENT

in my rails app related to the coffeescript was very frustrating, and difficult to debug because all the indentation in my code appeared to be correct.  It actually had to do with our my text editor, Sublime Text, was handling tabs and spaces.

To fix this, go to View -> Indentation and check Indent Using Spaces and Convert Indentation to Spaces.

Credit here.

Play Audio File on Button Click in Rails

So you want to play an audio clip on a button click.  In your view file

<%= audio_tag "audio.m4a", class: "audio-play" %>
<p class="btn btn-primary audioButton">Play</p>

This uses the rails audio tag, and then places a button next to it. In the javascript, you play the audio file on the button click:

$(".audioButton").on("click", function() {
  $(".audio-play")[0].currentTime = 0;
  return $(".audio-play")[0].play();

Or in coffeescript:

$(".audioButton").on "click", ->
  $(".audio-play")[0].currentTime = 0

Some credit to this Stack Overflow question.

Video End Event in Rails

How do you call an event or action after a video has finished playing? For example, I wanted a “Continue” button to appear after the video has finished.  Searching around this was a bit hard to find, so hopefully this post will help some people.

There is an “onended" function that you can call on the video object. This is how I do it.

Here’s the jQuery

$("#myVideo").bind("ended", function() {
   //whatever you want to do when the video ends

In Rails, here’s the coffeescript I use

$("#myVideo").bind "ended", ->
    # whatever you want to do when the video ends

Make sure the video tag in the html has the id that you’re referencing in the javascript/coffeescript.

<%= video_tag('FMCintro.mp4', size: "724x500", controls: true, id: "myVideo") %>

credit to this StackOverflow question. This one might be helpful too.