views:

65

answers:

4

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.

A: 

i did solve this problem, but i'm not sure if that is the best approach.

in my _form.html.erb i added the following statement:

<script type="text/javascript">
<%= get_js_as_string("rules_form")%>
</script>

then i created a helper function named get_js_as_string:

def get_js_as_string(jsname)
 filename = "#{RAILS_ROOT}/public/javascripts/#{jsname}"
 filename = filename + ".js" unless filename =~ /\.js$/ 

 data = ''
 f = File.open(filename, "r") 
 f.each_line do |line|
   data += line
 end
 return data
end

Then it worked! Am I reinventing the wheel? It was a good approach to keep just view code on my views and move all js to external js files..

VP
What does your "rules_form.js" look like? Does it expect to be executed right away? Or is it tucked into a $(document).ready(function() {}); ??? I think it it's not in a doc/ready then it would get executed before jQuery is available.
Jesse Wolgamott
the rules_form.js is a code that are inside of a $(document).ready(function() {}); block.
VP
A: 

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  A: 

I had a very similar question recently, only I was trying to load jQuery itself. The answer might help you: http://stackoverflow.com/questions/3129451/load-jquery-in-a-js-then-execute-a-script-that-depends-on-it

jasonpgignac
+1  A: 

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
it would give me a error or the script wouldnt be available as i wrote there.. anyway thanks
VP
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
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.
Andy Atkinson