Reputation: 2099
I have page1, page2 and page3 in my Rails application.
For page2 I have a special javascript file with some jQuery .click events.
I include the file to my application.js //= require specialfile
The specialfile is in my app/assets/javascripts/specialfile.js path.
I figured out that if I will go from page1 or page3 to the page2, the special js file doesn't work unless I will refresh the page. It works only if I will go directly to the page2 without any previous move.
I tried not to require the specialfile.js in my application.js pipeline and include it directly to my page2 by: <%= javascript_include_tag "specialfile" %>
. It didn't work. I also tried to add there the full path like assets/javascript/specialfile.js ... app/assets/javascript/specialfile but no luck.
How can I do it?
I found something like: <%= javascript_include_tag params[:controller] if AppName::Application.assets.find_asset("#{params[:controller]}.js") %>
but I don't understand well. How I can point to the specialfile?
Upvotes: 1
Views: 667
Reputation: 27961
By default Rails includes the turbolinks gem, which loads just the body
of the page when you click on a link without doing a full load of the whole page. Because it's no longer doing a full reload of the page, the "ready" event on document
is not triggered.
You should read the turbolinks README, especially this bit on Events for more information, but generally the quick way around this is instead of your code being in a $(...
or $(document).ready
block you should put it in:
$(document).on("ready page:load", ...
Upvotes: 2