# Populate data in another fields on selection

**URL:** <https://www.ruby-forum.com/t/populate-data-in-another-fields-on-selection/124278>\
**Category:** Rails\
**Created:** [December 27, 2007, 9:21am UTC](https://www.ruby-forum.com/t/populate-data-in-another-fields-on-selection/124278 "2007-12-27T09:21:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [December 27, 2007, 9:21am UTC](https://www.ruby-forum.com/t/populate-data-in-another-fields-on-selection/124278/1 "2007-12-27T09:21:19Z")

</div>

Hi,  
I would like to know how to populate data in another text fields on  
selection from a drop down

For Ex

1. The form contains 4 fields out of which 3 are text fields and the 4th  
one is  
drop down list.
2. i would like to know how to populate the 3 text fields with data  
simultaneously  
on selection from the drop down list.

Please Help.

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [December 27, 2007, 9:48am UTC](https://www.ruby-forum.com/t/populate-data-in-another-fields-on-selection/124278/2 "2007-12-27T09:48:24Z")

</div>

Koli K. wrote:

> Hi,  
> I would like to know how to populate data in another text fields on  
> selection from a drop down
> 
> For Ex
> 
> 1. The form contains 4 fields out of which 3 are text fields and the 4th  
> one is  
> drop down list.
> 2. i would like to know how to populate the 3 text fields with data  
> simultaneously  
> on selection from the drop down list.
> 
> Please Help.

Try using observe\_field!

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [December 27, 2007, 10:02am UTC](https://www.ruby-forum.com/t/populate-data-in-another-fields-on-selection/124278/3 "2007-12-27T10:02:12Z")

</div>

In yr view file add this

In View:

\<%= observe\_field ‘field\_id’,  
:url =\> url\_for({:controller=\>  
‘yr\_controller\_name’,:action=\>‘some\_method\_in\_controller’}),  
:method =\> :post,  
:with =\> ‘field\_id’ %\>

In Controller

def some\_method\_in\_controller  
#…  
some code to find the objects to be populated in the text fields.  
…#

page.replace\_html ‘div\_id1’, text\_field\_tag(‘text\_field\_id\_1’,@object)  
page.replace\_html ‘div\_id2’, text\_field\_tag(‘text\_field\_id\_2’,@object)  
page.replace\_html ‘div\_id3’, text\_field\_tag(‘text\_field\_id\_3’,@object)

end

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [December 28, 2007, 6:21am UTC](https://www.ruby-forum.com/t/populate-data-in-another-fields-on-selection/124278/4 "2007-12-28T06:21:49Z")

</div>

> def some\_method\_in\_controller  
> #…  
> some code to find the objects to be populated in the text fields.  
> …#
> 
> page.replace\_html ‘div\_id1’, text\_field\_tag(‘text\_field\_id\_1’,@object)  
> page.replace\_html ‘div\_id2’, text\_field\_tag(‘text\_field\_id\_2’,@object)  
> page.replace\_html ‘div\_id3’, text\_field\_tag(‘text\_field\_id\_3’,@object)
> 
> end

This didn’t work though can u be more descriptive??

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [December 28, 2007, 8:53am UTC](https://www.ruby-forum.com/t/populate-data-in-another-fields-on-selection/124278/5 "2007-12-28T08:53:37Z")

</div>

Ratnavel S. wrote:

> wat does it show in development log…?
> 
> can u paste the error log…

i have 2 tables entries and problems which is associated with each  
other.

The drop down list is poulated with one of the fields of the problems  
table:  
i’m able to that corresponding id into the entries table.

i have 3 other text fields which shoould be populated with the  
information  
of the id that i have selected from the drop down  
This is the part that i’m not able to do.

MY view:  
\<%= error\_messages\_for ‘entry’ %\>  
\<% @cats = Problem.find\_all %\>

Nature of the problem

 \<% @cats.each do |cat| %\> \> \<%= cat.natureofproblem %\> \<%end%\> TEXT FIELD1 TEXT FIELD2

How to populate these text field with the id info that i have from  
select dropdown.

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [December 28, 2007, 6:31am UTC](https://www.ruby-forum.com/t/populate-data-in-another-fields-on-selection/124278/6 "2007-12-28T06:31:25Z")

</div>

wat does it show in development log…?

can u paste the error log…

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [December 31, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/populate-data-in-another-fields-on-selection/124278/7 "2007-12-31T12:20:43Z")

</div>

Koli K. wrote:

> Ratnavel S. wrote:
> 
> > wat does it show in development log…?
> > 
> > can u paste the error log…  
> > cat| %\>  
> > \<option value=\<%=cat.id%\> \> \<%=  
> > cat.natureofproblem %\>   
> > \<%end%\>
> 
> TEXT FIELD1 TEXT FIELD2
> 
> How to populate these text field with the id info that i have from  
> select dropdown.
> 
> where do i do the page.replace\_html\>
> 
> can u show me a example\>\>
