title: Making a table row into a link in Rails using Coffeescript
body: <p>This is related to the following question but I would like to use coffeescript instead of javascript for comparability with the defaults in Rails 4.</p>
  
  <p>The starting point is code provided in answers to an earlier question: <a href="https://stackoverflow.com/questions/9945620/making-a-table-row-into-a-link-in-rails">Making a table row into a link in Rails</a></p>
  
  <p>The view ERB code contains a table generator with the following code:</p>
  
  <pre><code>&lt;% @songs.each do |song| %&gt;
    &lt;tr data-link="&lt;%= edit_song_path(song) %&gt;"&gt;
      &lt;td&gt;&lt;%= song.Name %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= song.Group %&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The <code>&lt;tr data-link="&lt;%= edit_song_path(song) %&gt;"&gt;</code> line is based on the answer given in the above linked question.</p>
  
  <p>I have the follwing in the songs.js.coffee file:</p>
  
  <pre><code>$("tr[data-link]").click -&gt; 
    window.location = this.dataset.link
  </code></pre>
  
  <p>This is my translation for the javascript in the answer from Mark Berry. I have also tried the other suggestions such as <code>window.location = $(this).data("link")</code>.</p>
  
  <p>When the page is loaded the table rows are not clickable. If I replace the action of setting the <code>window.location</code> with an alert, I get the alert dialog when the page loads and not when the user clicks a table row.</p>
  
  <p>The above coffeescript code is the only code in the song.js.coffee file. Am I missing anything that needs to be wrapped around the code?</p>
  
  <p>Partial answer: Adding <code>$ -&gt;</code> before the original coffeescript code as suggested in the first comment does make the table rows selectable ONCE. I believe from other reading that the dollar sign variable has been made an alias to jQuery (an example of this is in the idioms chapter of The Little Book on Coffeescript: <a href="http://arcturo.github.io/library/coffeescript/04_idioms.html" rel="nofollow noreferrer">http://arcturo.github.io/library/coffeescript/04_idioms.html</a>). </p>
  
  <p>Unfortunately, after the page is loaded one table row can be selected causing the edit page to be displayed, but if the user returns to the page, either using the back button on the browser or a link back to the page, the table rows are no longer selectable.</p>
  
  <p>The new coffeescript code is:</p>
  
  <pre><code>$ -&gt;
    $("tr[data-link]").click -&gt; 
      window.location = this.dataset.link
  </code></pre>
  
  <p>Is the above code the correct translation from javascript to coffeescript. I am new to both scripting languages.</p>
  
  <p>Is there a different answer to making a clickable table row using coffeescript in Rails 4?</p>
  
  <p>Thank you.</p>