## 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 <div id="list_doctors"> <%= render :partial => ''doctor'', :collection => @doctors %> </div> ## _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? -- Posted via http://www.ruby-forum.com/.
Ajaya Agrawalla
2006-Jul-21 02:45 UTC
[Rails] Re: How to display data using helpers and collections
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 > <div id="list_doctors"> > <%= render :partial => ''doctor'', :collection => @doctors %> > </div> > > ## _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 -- Posted via http://www.ruby-forum.com/.
Rath ---
2006-Jul-21 03:10 UTC
[Rails] Re: How to display data using helpers and collections
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-- Posted via http://www.ruby-forum.com/.
Rath ---
2006-Jul-21 03:58 UTC
[Rails] Re: How to display data using helpers and collections
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 <input id="doctor_given_names" name="doctor[given_names]" type="text"> 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 <input id="doctor_1_given_names" name="doctor[1][given_names]" type="text"> or <input id="doctor??_given_names" name="doctor??[given_names]" type="text"> 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-- Posted via http://www.ruby-forum.com/.
Ajaya Agrawalla
2006-Jul-21 04:09 UTC
[Rails] Re: How to display data using helpers and collections
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 > <input id="doctor_given_names" name="doctor[given_names]" type="text"> > > 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 > <input id="doctor_1_given_names" name="doctor[1][given_names]" > type="text"> > or > <input id="doctor??_given_names" name="doctor??[given_names]" > type="text"> > > 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. >> >> ajYou 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. -- Posted via http://www.ruby-forum.com/.
Dan Kohn
2006-Jul-21 04:11 UTC
[Rails] Re: How to display data using helpers and collections
<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: <input type="text" id="person_<%= @person.id %>_name" name="person [<%= @person.id %>][name]" value="<%= @person.name %>" /> - dan -- Dan Kohn <mailto:dan@dankohn.com> <http://www.dankohn.com/> <tel:+1-415-233-1000> On Jul 20, 2006, at 8:57 PM, 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 > <input id="doctor_given_names" name="doctor[given_names]" type="text"> > > 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 > <input id="doctor_1_given_names" name="doctor[1][given_names]" > type="text"> > or > <input id="doctor??_given_names" name="doctor??[given_names]" > type="text"> > > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails