Render javascript (jquery) dynamically
I'm using jquery datepicker(). In my index, I'm looping over: @tasks
using a partial
<%= render :partial => 'tasks/task', :collection => @tasks %>
In my task partial, I am assigning each task it's own parent div with an ID unique to each task, so:
<div id="task_<%= task.id %>">
<%= task.desc %>
</div>
As it stand (and it works but it's obtrusive), I can call a partial containing javascript at the bottom of the task partial like so:
<%= render :partial => 'tasks/task_javascript.js.erb', :locals => {:task => task}
Which contains some code that gets generated on the fly for each task. All works well, but it's obtrusive.
What is a better way to dynamically create some jquery code to hook into a task, which needs to be unique. I can't add a class to each task and call the datepicker() option on all items with 开发者_开发技巧that class because it only seems to work for the first item. The hook has to be on a unique ID.
A second sidenote question is: Why is rails rendering the javascript partial as inline HTML unless I add script tags?
<script>...javascript goes here...</script>
Can't you, as you said, give each item a common class, then iterate on these items, check for their id, then hook the datepicker to it?
精彩评论