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
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/
action_view/helpers/prototype_helper_rb.html
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!

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