# Link\_to\_function variable increment

**URL:** <https://www.ruby-forum.com/t/link-to-function-variable-increment/175188>\
**Category:** Rails\
**Created:** [September 9, 2009, 12:21pm UTC](https://www.ruby-forum.com/t/link-to-function-variable-increment/175188 "2009-09-09T12:21:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![prabesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f05b48/32.png) [@prabesh](https://www.ruby-forum.com/u/prabesh)\
**Post date:** [September 9, 2009, 12:21pm UTC](https://www.ruby-forum.com/t/link-to-function-variable-increment/175188/1 "2009-09-09T12:21:41Z")

</div>

My view looks like this  
\<%count=0%\>  
\<%=link\_to\_function(“add another”, nil, :id =\> “more\_link”) do |  
page|  
page.insert\_html :bottom, ‘phone\_numbers’, add\_phone\_field(count  
+=1)  
end  
%\>  
add\_phone\_field is a helper method.I need to increment count on every  
click.But my code above does not do that.  
Once count gets the value 1 it remains one.  
How can I increase the value of count on every click.

My helper method :  
def add\_phone\_field(num)  
html=%{

  
Phone number  
}  
html\<\< (fields\_for :contact\_phones do |ff|  
ff.text\_field :phone\_number,:index=\>num  
end )  
html\<\<link\_to\_function(“delete”, “$(‘phone#{num}’).remove()”)  
html\<\<%{
}  
end

---

<div class="post-metadata">

**Author:** ![prabesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f05b48/32.png) [@prabesh](https://www.ruby-forum.com/u/prabesh)\
**Post date:** [September 9, 2009, 12:43pm UTC](https://www.ruby-forum.com/t/link-to-function-variable-increment/175188/2 "2009-09-09T12:43:56Z")

</div>

Classic problem, please see  
[GitHub - ryanb/complex-form-examples: Various ways to handle multi-model forms in Rails.](http://github.com/ryanb/complex-form-examples/tree/master) and this  
[http://sifent.com/blog/2009/05/28/rails-nested-forms-jquery/](http://sifent.com/blog/2009/05/28/rails-nested-forms-jquery/)

Essentially, what you need to do is, use :child\_index =\> ‘NEW\_RECORD’  
and on click replace NEW\_RECORD by some unique number (time will be  
most appropriate) using javascript.

## Thanks, Abhinav

à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)
