1

My ruby on rails application has datatables; however, they only appear after the user goes to the page and refreshes once. It doesn't appear initially when then they go to the page.

Here is the javascript that's run prior to the page loading (because it's before the html content);

(function() {
  jQuery(function() {
    return $('#items').dataTable();
  });

}).call(this);

However, I thought using this may help, but it didn't:

$(document).ready(function() {
  $('#items').dataTable();
});

Any suggestions on how to accomplish this goal easily?

4
  • What do your view and controller look like? Is the Javascript in a separate .js file or in your view code? Does the page render fine without calling .dataTable()? Commented Oct 30, 2014 at 14:34
  • The javascript is in the assets/whatever.js file and the page also lodas fine without calling .dataTable(). The content in the examples above are located in the js file. Commented Oct 30, 2014 at 14:38
  • Try to do an "inspect element" after the page loads for the first time and see if there's any warning/error showing up. Commented Oct 30, 2014 at 14:57
  • A friend suggested that to me too, and no warnings or anything popped up in the console. Thanks for the suggestion though. Commented Oct 30, 2014 at 14:57

1 Answer 1

2

Whenever this happens to me, I find the culprit is usually Turbolinks. You should be able to use one of two options to get around it.

The first is this workaround for your js which has been passed around SO quite a bit.

var ready;

ready = function(){
  //your js scripts go here
};

$(document).ready(ready);
$(document).on('page:load', ready);

Or you can update the link_to helper you are using to get to this page to skip turbolinks when loading the page:

<%= link_to "Where you're going", add_product_path, data: { no_turbolink: true } %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.