# Swap Values via Model in an onChange Event Within submit\_tag Helper?

**URL:** <https://www.ruby-forum.com/t/swap-values-via-model-in-an-onchange-event-within-submit-tag-helper/137511>\
**Category:** Rails\
**Created:** [May 13, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/swap-values-via-model-in-an-onchange-event-within-submit-tag-helper/137511 "2008-05-13T20:50:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [May 13, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/swap-values-via-model-in-an-onchange-event-within-submit-tag-helper/137511/1 "2008-05-13T20:50:11Z")

</div>

I’m new to RoR and (and programming) and am having trouble wrapping my  
head around how to get this working. What I’m trying to do is autofill  
a web form with values I select from a drop-down menu. I want to pull  
these values in from my Openers table.

My DB Model is called Openers and has the following fields:

id  
description  
subject  
body

My Controller is called bar\_controller.rb and contains only 1 method  
which pulls all the records from Openers and orders them by  
description:

class BarController \< ApplicationController

def bar  
@show\_openers = Opener.find(:all, :order =\> “description”)  
end

end

I have 3 Views located user app/views/bar that make up a 2 column  
framed page. The first file is called index.html.erb and sets up the  
frames:

The second file is called bar.html.erb and fills in the upper portion  
(top 10% of the page).

\<%= javascript\_include\_tag :defaults %\>

\<% form\_for :bar, :url =\> { :action =\> :bar } do |form| %\>  
\<%= collection\_select(“openers”, “opener\_id”,  
@show\_openers, :id, :description ) %\>  
\<%= submit\_tag “insert opener”,  
:onClick =\>  
“parent.formtofill.document.form1.messagesubject.value=‘HERE\_IS\_SUBJECT’,  
parent.formtofill.document.form1.messagebody.value=‘HERE\_IS\_BODY’” %\>  
\<% end %\>

The last file is called formtofill.html.erb and fills in the bottom  
90% of the page. It contains the form that I want to auto-populate  
with the values defined in bar.html.erb.

Subject:

Body:

Right now I’ve got the descriptions populating the dropdown and the  
onClick will stuff the static HERE\_IS\_SUBJECT and HERE\_IS\_BODY into  
the correct form fields, but there are still a few things I’m having  
trouble with:

1. How do I access the subject and body info?
2. Once question 1 is solved, how do I update the HERE\_IS\_SUBJECT and  
HERE\_IS\_BODY values on an onChange event (within the submit\_tag  
helper) with the matching Openers.subject and Openers.body values for  
the Openers.description selection?

Thanks in advance for any help!!!
