# Pop Up Window

**URL:** <https://www.ruby-forum.com/t/pop-up-window/112545>\
**Category:** Rails\
**Created:** [August 20, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/pop-up-window/112545 "2007-08-20T20:09:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_ESHubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@Nicholas\_ESHubbard](https://www.ruby-forum.com/u/Nicholas_ESHubbard)\
**Post date:** [August 20, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/pop-up-window/112545/1 "2007-08-20T20:09:59Z")

</div>

Hey Guys, first post here.

I am running into some basic problems. I am trying to submit a form in  
a popup window and if the object saves close the popup window and  
refresh the page in the main window.

Has anyone done this, or know a good way to do this?

Sorry for such a stupid question, but I have ran around in circles  
trying to do this.

Thanks

---

<div class="post-metadata">

**Author:** ![Nicholas\_ESHubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@Nicholas\_ESHubbard](https://www.ruby-forum.com/u/Nicholas_ESHubbard)\
**Post date:** [August 20, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/pop-up-window/112545/2 "2007-08-20T20:27:03Z")

</div>

The best way to do it, in my mind, is to use an rjs template. In  
your controller, your action would look something like this:

def my\_action

# do usefull stuff.

…

```
 render :update do | page |
    if @something.save
        page << "myCloseWindowJavaScript()"
    else
        page << "notifyUserOfError()"
    end
 end

```

end

where myCloseWindowJavaScript() and notifyUserOfError are javascript  
functions you define in a .js file. You can also look at the  
documentation for  
ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMeth  
ods

[http://api.rubyonrails.org/files/vendor/rails/actionpack/lib/](http://api.rubyonrails.org/files/vendor/rails/actionpack/lib/)  
action\_view/helpers/prototype\_helper\_rb.html

---

<div class="post-metadata">

**Author:** ![Nicholas\_ESHubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@Nicholas\_ESHubbard](https://www.ruby-forum.com/u/Nicholas_ESHubbard)\
**Post date:** [August 21, 2007, 8:49am UTC](https://www.ruby-forum.com/t/pop-up-window/112545/3 "2007-08-21T08:49:43Z")

</div>

> I am running into some basic problems. I am trying to submit a form in  
> a popup window and if the object saves close the popup window and  
> refresh the page in the main window.

rjs / ajax could work (above post), but don’t forget to use \<%  
form\_remote\_tag %\> rather than \<% start\_form\_tag %\> so the ajax works.  
this could be a problem if you want to upload pictures in the popup  
window (ajax does not allow this as of now) , so a different option  
could be the following:

first of all, make sure the window is a popup that was ‘popped-up’ from  
the MainWindow (if it is a target="\_blank" the js functions won’t work  
on it).  
then, render js text:

or something similar.  
hope this helps, and welcome to the forum!

🙂

---

<div class="post-metadata">

**Author:** ![Nicholas\_ESHubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@Nicholas\_ESHubbard](https://www.ruby-forum.com/u/Nicholas_ESHubbard)\
**Post date:** [August 21, 2007, 9:04am UTC](https://www.ruby-forum.com/t/pop-up-window/112545/4 "2007-08-21T09:04:38Z")

</div>

> ```
> render :update do | page |
> if @something.save
> page << "myCloseWindowJavaScript()"
> else
> page << "notifyUserOfError()"
> end
> end
> 
> ```
> 
> end

…wanted to fix myself;  
note that doing render :update is actually rendering the javascript (and  
not necessarily using ajax), so you should end up having something like

```
  render :update do | page |
     if @something.save
         page << 'window.opener.location.reload();'
         page << 'window.close();'
     else
         page << 'some_other_js_func();'
     end
  end

```

end
