Search code examples
javascriptjqueryruby-on-railsrubyheroku

JS, JQuery not working with heroku


I am new to web development. As the title states, I have been having an issue with my JS in Heroku. I am using ruby on rails 5.1 and when I push my project to Heroku master everything is working fine but the JavaScript. I have been googling this for the last 2 days. I have tried organizing the //= require etcetc, have tried modifying my code, have tried changing config.assets.compile = false to true and have tried like 4 to 5 different results here in StackOverflow, like(Javascript features work on localhost but not when deployed to Heroku). Really don't know what else can I do. Here is my application.js

//= require jquery
//= require rails-ujs
//= require turbolinks
//= require jquery.easing
//= require bootstrap-sprockets
//= require creative
//= require_tree .

$(document).on('turbolinks:load', function() {
// Offset for Main Navigation
$('#mainNav').affix({
    offset: {
    top: 50
}
});

$("#button-transform").click(function() {
  $('.transform').toggleClass('transform-active');
});

window.setTimeout(function() {
$("#flash").fadeTo(2500, 0).slideUp(1750, function() 
{
    $(this).remove();
});
}, 3000);

});

Please let me know if you need more info.

Thanks


Solution

  • I managed to solve my problem. Eventually, after hours of research, I gave up about this issue and start doing some CSS style. When I tried to push the new CSS to Heroku I had the same issue. Not showing the updated CSS in Heroku. Again, did some research and found this;

    bundle exec rake assets:precompile
    

    At first, it did not work for me. It was saying that I needed to install yarnpkg, which I had already. So, did some more research and finally notice that I was not able to do the bundle exec rake assets:precompile because I was in a branch repository and not the master. Did a checkout to the master, ran the command again, checked the CSS and it was working. as well as the JQuery. To answer my question,

    bundle exec rake assets:precompile
    

    was the command that helped me.

    The steps are

    bundle exec rake assets:precompile
    git add --all
    git commit -m "some message"
    git push heroku master