1

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>

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

That never really occurred to me - going to give that a shot now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.