The question
My javascript isn't being called following a redirect_to. Is this expected? Is there a way to make it get called?
The controller
# file: app/controllers/widgets_controller.rb
class WidgetsController < ApplicationController
# GET /widgets
def index
@widgets = Widget.order(:id)
end
# GET /widgets/refresh_all
def refresh_all
@widgets = Widget.order(:id)
@widgets.refresh_all
redirect_to :widgets
end
end
The view
In /app/views/widgets/index.html.erb
:
<p>Widgets are <%= @widgets.any_refreshing? ? "" : "not" %> being refreshed.</p>
<script type="text/JavaScript">
$( window ).load(function() {
console.log( "...C" );
});
$( document ).ready(function() {
console.log( "...B" );
});
console.log("...A" );
</script>
What I observe
When I access the page via GET /widgets, the javascript is triggered and I see "...A\n...B\n...C\n" on the console. When I access the page via GET /widgets/refresh_all, the page is correctly rendered ("Widgets are being refreshed"), but nothing in the javascript is called.
Is this perhaps because I'm doing a redirect_to :widgets
in my controller?
What am I missing?