How to display data using helpers and collections

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?

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

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

Ok I found a solution :slight_smile:
<%= 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

Rath — wrote:

Ok I found a solution :slight_smile:
<%= 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.

<Peak Obsession
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:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000