# How to copy a selected value into a text\_field

**URL:** <https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674>\
**Category:** Rails\
**Created:** [April 11, 2010, 4:20am UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674 "2010-04-11T04:20:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 11, 2010, 4:20am UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674/1 "2010-04-11T04:20:26Z")

</div>

In app\views\expenses\new.html.erb, I (mistakenly) replaced the  
script/generated:  
\<%= f.label :vendor %\>  
  
\<%= f.text\_field :vendor %\>  
with:  
\<%= f.label :vendor %\>  
  
\<% @current\_vendors = Vendor.find(:all, :order=\>“nickname”) %\>  
\<%= select\_tag “list”, options\_for\_select(@current\_vendors) %\>

This (if I haven’t introduced any typos here) works fine, except that  
the user-selection doesn’t really populate the database’s vendors  
column in the expense record.

How do I repair this situation? I’d like to restore the the original  
text field and somehow populate it with the user-selected value from  
the select control.

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 12, 2010, 8:11am UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674/2 "2010-04-12T08:11:29Z")

</div>

RichardOnRails wrote:

> In app\views\expenses\new.html.erb, I (mistakenly) replaced the  
> script/generated:  
> \<%= f.label :vendor %\>  
>   
> \<%= f.text\_field :vendor %\>  
> with:  
> \<%= f.label :vendor %\>  
>   
> \<% @current\_vendors = Vendor.find(:all, :order=\>“nickname”) %\>  
> \<%= select\_tag “list”, options\_for\_select(@current\_vendors) %\>
> 
> This (if I haven’t introduced any typos here) works fine, except that  
> the user-selection doesn’t really populate the database’s vendors  
> column in the expense record.
> 
> How do I repair this situation? I’d like to restore the the original  
> text field and somehow populate it with the user-selected value from  
> the select control.

Hi,  
try this \<%= select\_tag “expense\_vendor”,  
options\_for\_select(@current\_vendors) %\>.

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 12, 2010, 10:12am UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674/3 "2010-04-12T10:12:43Z")

</div>

hi RichardOnRails

Try this

\<%= select\_tag ‘expense[vendor]’,  
options\_for\_select(@current\_vendors) %\>

cheers,  
sameera

On Apr 11, 7:18 am, RichardOnRails

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 12, 2010, 10:01pm UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674/4 "2010-04-12T22:01:11Z")

</div>

PRiyanka and sameera: Thank you both very much!!! I’ve been pulling  
my hair out for about a week with this thing.

I didn’t realize that the first argument of the select\_tag function  
apparently identifies the target  
of the selected item in the format “table\_name[column\_name]”.

Now that you’ve given me this solution, I have to try to implement  
what seems to me to by the more proper architecture for the Expense/  
Vendor relationship, namely have the drop-down populate a hidden text  
field (representing a expense-table column “vendor\_id”) and have the  
vendor name displayed in the expense page but be merely non-editable  
data not stored in the database. Probably a “has” clause would come  
into play here.

I’m going to take a stab at this, run into a blank wall again, and  
finally post a more thoughtfully worded question.

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 13, 2010, 6:15pm UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674/5 "2010-04-13T18:15:57Z")

</div>

Hey, Priyanka and Sameera,

I’ve thought about design I’m aiming for and realized that I can  
accomplish it in a very straight-forward way.

Again, thanks for your great solution to my original problem.

Best wishes,  
Richard

On Apr 10, 10:18 pm, RichardOnRails

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 13, 2010, 7:28pm UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674/6 "2010-04-13T19:28:35Z")

</div>

On Apr 12, 8:59 pm, RichardOnRails  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> PRiyanka and sameera: Thank you both very much!!! I’ve been pulling  
> my hair out for about a week with this thing.
> 
> I didn’t realize that the first argument of the select\_tag function  
> apparently identifies the target  
> of the selected item in the format “table\_name[column\_name]”.

you could have dodged that if you’d gone with f.select :vendor,  
choices  
The second parameter to f.select has the same requirements as what you  
give to options\_for\_select

Fred

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 14, 2010, 1:04pm UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674/7 "2010-04-14T13:04:00Z")

</div>

On Apr 13, 8:03 pm, RichardOnRails  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> selecting an item did not populate the vendor text control, as the  
> former solution did, some I’m sure I misunderstand your intent.

It’s just a drop down box - it’s not going to do anything to any other  
control on the page - just pointing out that you don’t need to use  
select\_tag when you have a form builder handing around.

Fred

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 15, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674/8 "2010-04-15T18:55:31Z")

</div>

Hi Fred,

I employed your code and it looks/works great. I could swear my old  
code populated an adjacent text-box, but I must have been  
hallucinating.

Thank for you additional post which jump-started my effort for neater  
code.

Best wishes,  
Richard

On Apr 14, 7:02 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 13, 2010, 9:04pm UTC](https://www.ruby-forum.com/t/how-to-copy-a-selected-value-into-a-text-field/186674/9 "2010-04-13T21:04:35Z")

</div>

Hi Frederick,

Thanks for your additional help with a simpler solution. I tried to  
replace my select\_tag approach with the f.select approach:

```
<%= f.select :vendor, @current_vendors %>
<%# = select_tag "expense[vendor]",
  options_for_select(@current_vendors) %>

```

1. The f.select :vendor provided the drop-down control, but  
selecting an item did not populate the vendor text control, as the  
former solution did, some I’m sure I misunderstand your intent.
2. I haven’t found documentation for the precise thing you  
recommended, but I’m hopeful I’ll have some luck with  
[ActionView::Helpers::FormHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)  
and/or [Action View Form Helpers — Ruby on Rails Guides](http://guides.rubyonrails.org/form_helpers.html) when I get time  
for careful study.

Best wishes,  
Richard

On Apr 13, 1:26 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
