# Counter for complex forms

**URL:** <https://www.ruby-forum.com/t/counter-for-complex-forms/182231>\
**Category:** Rails\
**Created:** [January 25, 2010, 2:20pm UTC](https://www.ruby-forum.com/t/counter-for-complex-forms/182231 "2010-01-25T14:20:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoRoRo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@RoRoRo](https://www.ruby-forum.com/u/RoRoRo)\
**Post date:** [January 25, 2010, 2:20pm UTC](https://www.ruby-forum.com/t/counter-for-complex-forms/182231/1 "2010-01-25T14:20:25Z")

</div>

Hi, I’m developing a daily report module for a school work that  
utilizes complex forms, however I’ve run into some problems. On each  
rendered partial, I have two collection\_select; the first (project)  
updates the contents of the second (task) depending on the selected  
project. I need a counter to connect to the task div id so that each  
project collection\_select know which task collection\_select to update.  
Here is a part of the code in the partial:

_day\_entry.html.haml:  
%td  
= day\_entry\_detail\_form.collection\_select(:project\_id,  
 @projects, :id, :proj\_code\_and\_name, {:include\_blank =\> “#  
{I18n.t :select\_one}”}, :onChange =\> "showTaskSelection($(this),  
'task\_form_\<counter\_goes\_here\>’), ‘1’")  
%th{:scope =\> “row”, :class =\> “column1”}  
= I18n.t :task  
%span{:style =\> ‘color:#FF0000’}\*  
%td  
%div{:id =\> “task\_form\_\<counter\_goes\_here\>”}  
- for project in @projects  
%div{:id =\> “project\_#{project.id}_task_”, :style =\>  
“display:none”}  
= day\_entry\_detail\_form.collection\_select(:task\_id,  
project.project\_task, :id, :task\_code\_and\_name, {:include\_blank =\> “#  
{I18n.t :select\_one}”})

and this code snippet renders the partial:

new.html.haml:  
#day\_entry\_details  
= render :partial =\> ‘day\_entry\_detail’, :collection =\>  
@day\_entry.day\_entry\_details, :locals =\> {:from\_collection =\> true}  
.form-btn  
= link\_to\_function “#{I18n.t :add\_entry}” do |page|  
- page.insert\_html :bottom, :day\_entry\_details, :partial =\>  
‘day\_entry\_detail’, :object =\> DayEntryDetail.new, :locals=\>  
{:from\_collection=\>false}

I need to have a counter that increments every time the  
link\_to\_function is used, and then use this counter as part of a div  
name. I tried using a hidden field and then just calls a javascipt  
function to increment it, but I don’t know how I’ll be able to access  
that for use with the div id…I’ve just started developing in rails  
and not yet familiar with everything. Any ideas or a push in the right  
direction would be a great help 🙂
