Javascript not executing after Rails redirect
<p>My application contains a very simple login and two separate AJAX calls. I have a loading spinner that is hidden automatically whenever a page is ready it is contained in index.js </p>

<pre><code>$(function() {

   $("loading").hide();

   $("#scan_button").on("ajax:success", function(e, data, status, xhr) {
       $("#standard_results").hide();
       $("#results").show();
       $("#results").html(data);
   });

   $(document).ajaxStop(function() {
       $("#loading").hide();
   });

   $(document).ajaxStart(function() {
       $('#loading').show();
   });

});
</code></pre>

<p>When a user logs in the following happens </p>

<pre><code>class SessionController &lt; ApplicationController

    def create
        user = User.find_by(username: params[:session][:username])

        if(user.password == params[:session][:password])
            log_in user
            redirect_to root_path
        end
    end

    def destroy
        logout
        redirect_to root_path
    end

end
</code></pre>

<p>The route path takes me back to 'home#index' for which the index.js file exists. When the user logs in the redirect happens and the loading spinner is hidden and all JavaScript executes as expected. </p>

<p>When the user hits log out however the redirect occurs, but the loading spinner is shown and no JavaScript ever gets executed. Here is my index.html.erb file that contains the JavaScript. </p>

<pre><code>&lt;% content_for :title, "Network Monitor" %&gt;


&lt;div class="container-fluid"&gt;

    &lt;nav class="navbar narvbar-dark bg-inverse"&gt;
        &lt;span class="navbar-brand"&gt;Network Monitor&lt;/span&gt;

        &lt;%= link_to "Scan", scan_path, remote: true, class: "btn btn-secondary", id: "scan_button" %&gt; 


        &lt;% if logged_in? %&gt;

            &lt;span class="pull-xs-right"&gt;&lt;%= link_to "Logout", logout_path, class: "btn btn-secondary" %&gt;&lt;/span&gt;

        &lt;% else %&gt;

            &lt;%= form_for(:session, url: login_path, :html =&gt; {:class =&gt; 'form-inline pull-xs-right'}) do |f| %&gt;

                &lt;%= f.text_field :username, class: 'form-control', placeholder: "Username" %&gt;
                &lt;%= f.password_field :password, class: 'form-control', placeholder: "Password" %&gt;
                &lt;%= f.submit "Log in", class: 'btn btn-primary', id: 'login_button' %&gt;

            &lt;% end %&gt;
        &lt;% end %&gt;
    &lt;/nav&gt;

    &lt;div id="loading"&gt;
        &lt;%= image_tag "loading.gif", class: "loading_gif" %&gt;  
    &lt;/div&gt;

    &lt;div class="row"&gt;
        &lt;div class="col-md-12"&gt;
            &lt;div id="scan_bar"&gt;
                &lt;h3&gt;Welcome to your network monitor!&lt;/h3&gt; &lt;br /&gt;

                &lt;p&gt;To get started hit the scan button in the top right corner, this will detect the computers that are alive on your network and show them below.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="row"&gt;
        &lt;div id="results"&gt;
    &lt;/div&gt;

  &lt;div id="standard_results"&gt;

  &lt;/div&gt;
</code></pre>

<p></p>

<p></p>