How to load jQuery dynamically with Rails?
I have a page, where I load a partial (that contains a form) dynamically.
So after that you clic开发者_StackOverflow社区k in a button, via jQuery I do a get, get the form and I change the html of a div. Everything works great.
The problem is I have some javascript that I added inline on the partial and I would like to remove it. So in my partial I made something like:
<% content_for :header %>
<%= javascript_include_tag 'myscript' %>
<%end%>
For sure in my layout I have the yield header. Actually I can see my scripting being included but the content of $(function(){});
get never executed. Any tip about it? Any better way to do it? I want to move my JavaScript to external js files but for now, I couldn't find a good way to do it.
I had a very similar question recently, only I was trying to load jQuery itself. The answer might help you: Load jQuery in a js, then execute a script that depends on it
Change
<% content_for :header %>
<%= javascript_include_tag 'myscript' %>
<%end%>
To (notice the 'do' in the content_for)
<% content_for :header do %>
<%= javascript_include_tag 'myscript' %>
<%end%>
The do is creating the builder, so I think before you weren't getting the [script src=] tags being added to your [head]
http://guides.rubyonrails.org/layouts_and_rendering.html#using-content-for
Are you using prototype of rails and Jquery simultaneously.If you are including both then the Jquery function will not run.Please check or let me know if any other issue.
精彩评论