title: Why doesn't JavaScript work when I redirect from Rails Controller?
body: <h2>Problem</h2>
  <p>Why doesn't JavaScript work when I redirect from Rails Controller?</p>
  <p>How do I modify it so that JavaScript works?</p>
  <h2>Code</h2>
  <p>sessions_controller.rb <strong>(Redirect by Create action)</strong></p>
  <pre class="lang-rb prettyprint-override"><code>class SessionsController &lt; ApplicationController
    ...
  
    def create
        ...
        redirect_to root_path
    end
  
    ...
  end
  </code></pre>
  <p>rails routes</p>
  <pre class="lang-sh prettyprint-override"><code>% rails routes | head
  Prefix Verb   URI Pattern Controller#Action
  root   GET    /           shops#index
  </code></pre>
  <p>shops_controller.rb</p>
  <pre class="lang-sh prettyprint-override"><code>class ShopsController &lt; ApplicationController
    def index
      ...
    end
  
    ...
  end
  </code></pre>
  <p>index.html.erb</p>
  <pre class="lang-html prettyprint-override"><code>...
    &lt;%= render partial: &quot;shops_sort_by_distance&quot;, locals: {area: area} %&gt;
  ...
  </code></pre>
  <p>index.js <strong>(Not executed)</strong></p>
  <pre><code>window.onload = () =&gt; {
      ...
      func();
  };
  </code></pre>