Working in Rails 4.2.5, I have the following code in my
application.js file:
$(document).ready(function() {
$("#property_selection #properties").click(function() {
var url = window.location.href
.replace(/\/?$|(\?|&)current_prop_id=[^?&]+/g, "")
+ "?current_prop_id="
+ $("#properties option:selected").val();
window.location.href = url;
})
});
I.e., when I select a property_id, I want to go to the same URL, plus
"?current\_prop\_id=", plus the ID.
This works the first time I login. When I visit a path like
http://localhost:3000/foo, however, it doesn't. Hitting refresh on
the page makes it work again.
Then I changed the $(document).ready line to
$(document).on('page:load', ..., (based on what I'd read
here),
but that only reversed the problem. Now it works the first time, but
not the second.
Dropping this code into a static file works just fine.
I now have both defined in application.js, 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.