# How to display data using helpers and collections

**URL:** <https://www.ruby-forum.com/t/how-to-display-data-using-helpers-and-collections/99226>\
**Category:** Rails\
**Created:** [July 21, 2006, 4:00am UTC](https://www.ruby-forum.com/t/how-to-display-data-using-helpers-and-collections/99226 "2006-07-21T04:00:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 21, 2006, 4:00am UTC](https://www.ruby-forum.com/t/how-to-display-data-using-helpers-and-collections/99226/1 "2006-07-21T04:00:20Z")

</div>

## application\_helper.rb

def name\_field(object, options = {} )  
html = “”  
html \<\< "Title : " + select(object, ‘title’)  
html \<\< “Given Names :” + text\_field(object, ‘given\_names’  
html \<\< “Family Name :” + text\_field(object, ‘family\_name’  
html  
end

## index.rhtml

 \<%= render :partial =\> 'doctor', :collection =\> @doctors %\>

## \_doctor.rhtml

\<%= name\_field “doctor#{doctor.id}” %\>

How do I display the data from the doctor?

I have try this and it does not work  
\<%= name\_field “doctor[#{doctor.id}]” %\>  
NameError: `@nok[1]’ is not allowed as an instance variable name

And  
\<%= name\_field “doctor\_#{doctor.id}” %\>  
But this does not display the data for the current doctor in the  
collection

Any ideas?

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 21, 2006, 4:44am UTC](https://www.ruby-forum.com/t/how-to-display-data-using-helpers-and-collections/99226/2 "2006-07-21T04:44:42Z")

</div>

Rath — wrote:

> ## application\_helper.rb
> 
> def name\_field(object, options = {} )  
> html = “”  
> html \<\< "Title : " + select(object, ‘title’)  
> html \<\< “Given Names :” + text\_field(object, ‘given\_names’  
> html \<\< “Family Name :” + text\_field(object, ‘family\_name’  
> html  
> end
> 
> ## index.rhtml
> 
> \<%= render :partial =\> 'doctor', :collection =\> @doctors %\>
> 
> ## \_doctor.rhtml
> 
> \<%= name\_field “doctor#{doctor.id}” %\>
> 
> How do I display the data from the doctor?
> 
> I have try this and it does not work  
> \<%= name\_field “doctor[#{doctor.id}]” %\>  
> NameError: `@nok[1]’ is not allowed as an instance variable name
> 
> And  
> \<%= name\_field “doctor\_#{doctor.id}” %\>  
> But this does not display the data for the current doctor in the  
> collection
> 
> Any ideas?

Do this within \_doctor.rhtml  
\<% @doctor = doctor %\>  
\<%= name\_field @doctor %\>

It will work.

aj

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 21, 2006, 5:09am UTC](https://www.ruby-forum.com/t/how-to-display-data-using-helpers-and-collections/99226/3 "2006-07-21T05:09:40Z")

</div>

Thanks aj

I tried that and now I got this error  
`@#Doctor:0x34f0f68’ is not allowed as an instance variable name

Any other idea?

Cheers

> Do this within \_doctor.rhtml  
> \<% @doctor = doctor %\>  
> \<%= name\_field @doctor %\>
> 
> It will work.
> 
> aj

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 21, 2006, 5:57am UTC](https://www.ruby-forum.com/t/how-to-display-data-using-helpers-and-collections/99226/4 "2006-07-21T05:57:50Z")

</div>

Ok I found a solution 🙂  
\<%= name\_field “doctor” %\>

But know, my question relates to the other one I did earlier in other  
post. How can I number the objects? I mean, this returns

but if I want many doctors in the page, all the fields will have the  
same ID, therefore I will not be able to differentiate between them.

I mean  
  
or

How can I do that?

Thanks

PS name\_field “doctor[1]” does not work

> Do this within \_doctor.rhtml  
> \<% @doctor = doctor %\>  
> \<%= name\_field @doctor %\>
> 
> It will work.
> 
> aj

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 21, 2006, 6:09am UTC](https://www.ruby-forum.com/t/how-to-display-data-using-helpers-and-collections/99226/5 "2006-07-21T06:09:42Z")

</div>

Rath — wrote:

> Ok I found a solution 🙂  
> \<%= name\_field “doctor” %\>
> 
> But know, my question relates to the other one I did earlier in other  
> post. How can I number the objects? I mean, this returns
> 
> but if I want many doctors in the page, all the fields will have the  
> same ID, therefore I will not be able to differentiate between them.
> 
> I mean  
>   
> or
> 
> How can I do that?
> 
> Thanks
> 
> PS name\_field “doctor[1]” does not work
> 
> > Do this within \_doctor.rhtml  
> > \<% @doctor = doctor %\>  
> > \<%= name\_field @doctor %\>
> > 
> > It will work.
> > 
> > aj

You mean your doctors collection has no ID in it?

for iteration based ID, you would have to keep it as a global variable  
as partial for collection means calling the partial many times with a  
single object.

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 21, 2006, 6:14am UTC](https://www.ruby-forum.com/t/how-to-display-data-using-helpers-and-collections/99226/6 "2006-07-21T06:14:02Z")

</div>

\<[Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/)  
FormHelper.html\> explains:

If the object name contains square brackets the id for the object  
will be inserted. Example:

\<%= text\_field “person[]”, “name” %\>

?becomes:

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000
