javascriptjqueryruby-on-rails

Clickable Table Rows in Ruby on Rails


Just getting started with Rails and I am trying to create clickable table rows in Rails using bootstrap tables. I have managed to do this, however there seems to be a problem where when I click on the row through to the show page, then press the back button on that page to return to the index view, the clickable rows do not work anymore. I need to refresh the page to get it to work again.

I have taken a look at Rails 3 - How to make entire table row clickable using jQuery along with other answers on the site. Still does not seem to work.

My code is as follows

Index Page

<tr data-link="<%= source_path(source) %>">
        <td><%= source.name %></td>
        <td><%= source.description %></td>
        <td><%= link_to truncate(source.url, :length => 40), source.url, :target => '_blank' %></td>
        <td><%= source.category %></td>
        <td><%= source.user.name if source.user %></td>
      </tr>

application.js

jQuery(function($) {
$("tr[data-link]").click(function() {
window.location = this.dataset.link
});
})

Solution

  • Try using jQuery data() method

    jQuery(function($) {
    
        $("tr[data-link]").click(function() {
            window.location = $(this).data('link');
        });
    
    });
    

    I don;t know why yours isn't working. Perhaps you have errors thrown in browser console