Just another Rails newbie with a problem.
I have some Javascript (dynamic) in a partial .html.erb, but I have been informed in another post that this is really not where the Javascript should go. It’s dynamic so I can find the correct ID of an element and only apply the javascript on that particular element. I assume it is possible to parameterise the javascript and put it in an external file and somehow call it from my partial. Would that be the correct way to do it? If so how would I do that? If not what should I do?
I have a nested resource. On the parent resource’s show I am using AJAX to add children without leaving this page. I have quite a lot of Javascript magic that needs to be executed for each added child.
Partial _care_point.html.erb
<script>
$(function() {
$("#<%="node_#{care_point.id}" %>").live('dblclick', function(){
console.log('moo');
jQuery(this).hide();
jQuery('.close', jQuery(this).parent()).show();
jQuery('.node_input', jQuery(this).parent()).show();
});
});
</script>
<div id=<%="care_point.#{care_point.id}" %> class='draggable node_chin'>
<div id=<%="node_#{care_point.id}" %> class='node'><%= care_point.body %>
</div>
<textarea class='node_input'><%= care_point.body %></textarea>
<a class='close' href='#'>Close</a>
</div>
Thanks,
Erik
In your partial:
In public/javascripts/application.js (where your js should be)
Having used rails for a few years, I have never had to generate JS dynamically in the view. I can’t think of a good use case that would require this (if you can think of one, leave a comment please).