submit form in popup and send value back to the main page

I made a popup within object1's form to show another form to create a new document object to associate with object1. When I click the submit button I want 1) the document object to be created 2) the popup to close 3) the value in the document "title" field to get added to the document name field in object1's form.

I've made the form for the document object in the popup but I'm having trouble figuring out the logic for 2) and 3). I'd imagine I have to use javascript to close the popup and send the title back to the main page.

I added some javascript to the create.html.erb template that opens in the popup after the document form submits. The javascript is supposed to close the popup and send the title of the document back to the main page but it's not working. Anyone know why?

this is what is in the create.html.erb template:

<script type="text/javascript">

function copyData(){     var docField = window.opener.document.getElementById('title');     docField.setValue('<%=h @doc.title %>');     this.close(); }

copyData();

</script>