title: How to implement coffeescript to work after a 401 ajaxerror?
body: <p>I'm building a reddit/angellist clone. I want to be able to upvote and downvote without the page reloading. I've the ajax setup via the votescontroller:</p>
  
  <pre><code>respond_to do |format|
      format.html { redirect_to request.referer }
      format.js
  end
  </code></pre>
  
  <p>and then in the upvote.js.erb:</p>
  
  <pre><code>$(document).ready(function(e) {
    $("#vote-sum").html("&lt;%= pluralize(@votable.votes.sum(:value), 'vote') %&gt;");
  });
  </code></pre>
  
  <p>So far all good. When clicking on up the sum instantly changes.</p>
  
  <p>This all happens when a user is logged in. When a user is not logged in, and you press the up link, I get a <code>401</code>, because I use Devise I have <code>before_action :authenticate_user!</code> in my votes controller. I get this as output on my local server:</p>
  
  <pre><code>Started POST "/items/11/upvote" for 127.0.0.1 at 2017-12-02 17:52:06 +0100
  Processing by VotesController#upvote as JS
    Parameters: {"item_id"=&gt;"11"}
    Item Load (1.2ms)  SELECT  "items".* FROM "items" WHERE "items"."id" = $1 ORDER BY "items"."created_at" DESC LIMIT $2  [["id", 11], ["LIMIT", 1]]
  Completed 401 Unauthorized in 24ms (ActiveRecord: 1.2ms)
  </code></pre>
  
  <p>I want to automatically send the user towards the sign in page. So I googled for answers and found one, with a great answer:</p>
  
  <p><a href="https://stackoverflow.com/a/10460074/6430382">https://stackoverflow.com/a/10460074/6430382</a></p>
  
  <p>I have to insert these lines of code:</p>
  
  <pre><code>$ -&gt;
    $("a").bind "ajax:error", (event, jqXHR, ajaxSettings, thrownError) -&gt;
      if jqXHR.status == 401 # thrownError is 'Unauthorized'
        window.location.replace('/users/sign_in') // or with .replace(this)
  </code></pre>
  
  <p>However when I add these lines it does not work. I changed the <code>$("a")</code> to <code>$(document)</code>. Or even changed this to vanilla JS:</p>
  
  <pre><code>$(function() {
    return $(document).bind("ajax:error", function(event, jqXHR, ajaxSettings, thrownError) {
      if (jqXHR.status === 401) {
        return window.location.replace('/users/sign_in');
      }
    });
  });
  </code></pre>
  
  <p>sidenote: a normal console.log('!') and alert('!') works fine in the <code>application.js</code> or the <code>votes.coffee</code> code.</p>
  
  <p>I expect the code to redirect a user towards the devise sign in page.</p>
  
  <p>Other similar resources I've looked at:</p>
  
  <p><a href="https://stackoverflow.com/a/34434921/6430382">https://stackoverflow.com/a/34434921/6430382</a></p>
  
  <p><a href="https://stackoverflow.com/a/9903564/6430382">https://stackoverflow.com/a/9903564/6430382</a></p>
  
  <p><a href="https://stackoverflow.com/questions/41050915/devise-authenticate-user-breaks-when-making-remote-true-requests-to-a-non-devi">Devise authenticate_user! breaks when making remote: true requests to a non-devise controller action</a></p>