title: Why isn't my coffee script function defined when I visit my page via a "link_to" link?
body: <p>I’m using <strong>Rails 4.2.7</strong>.  On a particular page (/users/edit), I have this link</p>
  
  <pre><code>&lt;%= link_to "Cancel", {:controller =&gt; :my_objects, :action =&gt; 'index' }, :class =&gt; 'btn' %&gt;
  </code></pre>
  
  <p>when I click on the link, I’m whisked away to the right page, in which I have a coffee script that is supposed to be executed.  It contains this</p>
  
  <pre><code>$ -&gt;
    #("#add_form).click -&gt;
      console.log(" invoking function load. ")
      setup_save_behavior()
  …
  setup_save_behavior = () -&gt;
    $("a.modal_button").unbind('click')
    $("a.modal_button").click -&gt;
      # Setup action
      $('#new_my_object').attr('action', '/my_objects/create')
      $('#_method').val('')
      # Clear existing error messages
      clear_form_errors()
      # Clear existing form elements
      $(':input[name^=my_object]','#new_my_object')
        .not(':button, :submit, :reset')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
  </code></pre>
  
  <p>What is strange is that when I click on my link above, I get the JS error on the page</p>
  
  <pre><code>my_objects.self-50d1edb….js?body=1:16 Uncaught TypeError: setup_save_behavior is not a function
  </code></pre>
  
  <p>even though the console.log is executed (I can see " invoking function load. " printed out).  If I actually go into my URL’s address bar and then type my URL, “<a href="http://loclahost:3000/my_objects/index" rel="nofollow">http://loclahost:3000/my_objects/index</a>,” then everything loads fine and there are no Javascript errors.  I’m totally baffled.  What can I do so that my function gets defined when I click on the link defined by the “link_to” script?</p>