# Unique identifier on render :partial with collection

**URL:** <https://www.ruby-forum.com/t/unique-identifier-on-render-partial-with-collection/117327>\
**Category:** Rails\
**Created:** [October 16, 2007, 12:21am UTC](https://www.ruby-forum.com/t/unique-identifier-on-render-partial-with-collection/117327 "2007-10-16T00:21:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [October 16, 2007, 12:21am UTC](https://www.ruby-forum.com/t/unique-identifier-on-render-partial-with-collection/117327/1 "2007-10-16T00:21:20Z")

</div>

Is there any way to pass a unique identifier to each iteration of a  
partial generated using the :collection option?

I have a form that contains a variable number of text fields that  
collect data into an array that’s processed in the controller upon  
submit. A text field can be added to the form using an AJAX request  
that adds a new blank object to a collection and re-renders the partial  
using the new collection. The thing is, I need to be able to remove  
text fields also, so they need to have a unique ID.

Is there some sort of unique identifier that is passed into the partial  
when you’re using the :collection option?

Andrew

---

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [October 16, 2007, 12:32am UTC](https://www.ruby-forum.com/t/unique-identifier-on-render-partial-with-collection/117327/2 "2007-10-16T00:32:11Z")

</div>

Andrew H. wrote:

> Is there any way to pass a unique identifier to each iteration of a  
> partial generated using the :collection option?
> 
> I have a form that contains a variable number of text fields that  
> collect data into an array that’s processed in the controller upon  
> submit. A text field can be added to the form using an AJAX request  
> that adds a new blank object to a collection and re-renders the partial  
> using the new collection. The thing is, I need to be able to remove  
> text fields also, so they need to have a unique ID.
> 
> Is there some sort of unique identifier that is passed into the partial  
> when you’re using the :collection option?
> 
> Andrew

Nevermind! I just found the “free” \_counter variable that is made  
available to each iteration of the partial.

---

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [October 16, 2007, 12:34am UTC](https://www.ruby-forum.com/t/unique-identifier-on-render-partial-with-collection/117327/3 "2007-10-16T00:34:35Z")

</div>

On Tue, 2007-10-16 at 00:21 +0200, Andrew H. wrote:

> Is there some sort of unique identifier that is passed into the partial  
> when you’re using the :collection option?
> 
> Andrew

There is a counter by the name of \<partial\_name\>\_counter, so  
render(:partial =\> ‘foo’, :collection =\> array) would have a  
foo\_counter.

–  
Tore D.  
removed\_email\_address@domain.invalid  
Trondheim, NO  
[http://tore.darell.no/](http://tore.darell.no/)
