Rails version 5.1.3
Specifically, it's the code in assets/javascripts/channels/my_channel.coffee
where channel js client code lives, with other general js code. I'm using actioncable. But general jQuery/javascript callbacks suffer from this, too. Rewriting jQuery append
in javascript didn't make difference.
All the callbacks are triggered twice. In other words, the fucntions to be executed after callbacks are triggered are called twice, including channel subscription and $(document).ready
. Concretely, if I put
$(some_id_name).append some_string
In the event function in $(document).ready
, it will be appended twice. I consider actioncable channel subscription instance as a callback, too. The receive
method is called by user sending messages to trigger the instance. So the code in that method is executed twice, too. append
in my case.
Could be entire js file is loaded twice. But how?
This only happens in test and production environment(Heroku), not in development environment. Implies anything?
I removed turbolinks
cleanly in 3 steps as you can find by searching online. Include jquery
and rails-ujs
exactly once, no jquery_ujs
, in application.js
. No public/assets
exists.
Part of my_channel.coffee
looks like the following. All custom functions contain several callbacks like click
, keydown
. If I use unbind
and bind
to them it will solve the problem. But I can't apply this to $(document).ready
and channel subscription instance.
$(document).ready ->
$('#messages').append "document is ready"
submitQuestion()
submitAnswer()
hideJudgeForm()
return
In application.js
//= require jquery
//= require rails-ujs
//= require bootstrap
//= require spin.js/spin
//= require_tree .