title: Rails controller method to save model and close popup window
body: <p>I have a web app that allows users to edit model data via a popup window. When the popup window form is completed, the user clicks a "Save" button, the window closes, and the original page reloads to show any updates that happened to the data.</p>
  
  <p>My technique only works sometimes. I can't for the life of me figure out why it works one time and why it doesn't another.</p>
  
  <p>In my popup window's form, I have this as the submit method:</p>
  
  <pre><code>&lt;%= submit_tag("Update Invoice", {:onclick=&gt;"refreshParent()"}) %&gt;
  </code></pre>
  
  <p>So the Javascript is executed ideally at the same time that the form is submitted to the controller. Here's the refreshParent() Javascript function:</p>
  
  <pre><code>function refreshParent(id) {
      window.opener.location.reload();
      window.close();
  }
  </code></pre>
  
  <p>Now, the method called by my form ("update" in this case) doesn't have a redirect_to method call, but even when I do (say, to the original view, index) it doesn't seem to matter.</p>
  
  <p>At root, it seems that my controller method doesn't fire on form submit. Sometimes. I can submit the same form data multiple times, and it'll work the third time. Or sometimes it works right away. It's totally random.</p>
  
  <p>What can I do to ensure that my controller methods fire when you close a popup window?</p>
  
  <p>Thanks,
  Aaron.</p>