0

I have a page, where I load a partial (that contains a form) dynamically.

So after that you click 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.

splattne
  • 102,760
  • 52
  • 202
  • 249
VP.
  • 5,122
  • 6
  • 46
  • 71
  • what does your `yield :header` and its surroundings look like, perhaps it's a matter of sequence of loading. – jigfox Jul 13 '10 at 13:34
  • my yield :header is on top, inside the tags. So when you click in the button to open a dialog with my form, it should add the javascript where it has the $(function(){}); code that should be executed when the form is loaded... – VP. Jul 13 '10 at 13:47

3 Answers3

1

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

Community
  • 1
  • 1
jasonpgignac
  • 2,296
  • 2
  • 19
  • 26
1

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

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • it would give me a error or the script wouldnt be available as i wrote there.. anyway thanks – VP. Jul 13 '10 at 20:08
  • OK, if you can post an example of the non-working HTML/script page we can diagnose. I think you reading and writing javascript into the HTML via File.open is a poor choice – Jesse Wolgamott Jul 14 '10 at 13:35
  • I have always done what Jess is recommending. We have all our JS files in separate JS files included in the head section this way, or included in the page wrapped in CDATA blocks. – Andrew Atkinson Sep 22 '10 at 04:18
0

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.

gsoni
  • 1,126
  • 15
  • 25