Rails 4 jquery function works once, not on reload
<p>Working in Rails 4.2.5, I have the following code in my
<code>application.js</code> file:</p>

<pre><code>$(document).ready(function() {
$("#property_selection #properties").click(function() {
    var url = window.location.href
            .replace(/\/?$|(\?|&amp;)current_prop_id=[^?&amp;]+/g, "")
        + "?current_prop_id="
            + $("#properties option:selected").val();
    window.location.href = url;
    })
});
</code></pre>

<p>I.e., when I select a property_id, I want to go to the same URL, plus
<code>"?current\_prop\_id="</code>, plus the ID.</p>

<p>This works the first time I login. When I visit a path like
<code>http://localhost:3000/foo</code>, however, it doesn't. Hitting refresh on
the page makes it work again.</p>

<p>Then I changed the <code>$(document).ready</code> line to
<code>$(document).on('page:load', ...</code>, (based on what I'd read
<a href="https://stackoverflow.com/questions/17381069/rails-jquery-doesnt-work-on-other-pages?rq=1">here</a>),
but that only reversed the problem. Now it works the first time, but
not the second.</p>

<p>Dropping this code into a static file works just fine.</p>

<p>I now have both defined in <code>application.js</code>, which feels wrong on
many levels, but I don't know what to do to get the function to work in
both cases otherwise. Anyone have any suggestions? Thanks.</p>