** File uploads with AJAX mojo ** Respond with RJS to your parent window with a form action targeted to a hidden IFRAME. Handles all the painful situations like scoping your JavaScript to your parent window generating the script block for execution and clearing the IFRAME after execution so the back button doesn''t re-execute the action. `plugin install http://sean.treadway.info/svn/plugins/ responds_to_parent` class EnterpriseElevatorPitchExampleController < ActionController::Base def iframe_target_action responds_to_parent do render :update do |page| page.hide :progress_bar page.show :success page.replace_html :success, ''Finished'' page.alert "RJS isn''t limited to XMLHttpRequest" end end end end Discuss, comment, and feedback for improvement: http://sean.treadway.info/articles/2006/05/29/iframe-remoting-made-easy RespondsToParent =============== Adds responds_to_parent to your controller to respond to the parent document of your page. Make Ajaxy file uploads by posting the form to a hidden iframe, and respond with RJS to the parent window. Example ====== Controller: class Test < ActionController::Base def main end def form_action # Do stuff with params[:uploaded_file] responds_to_parent do render :update do |page| page << "alert($(''stuff'').innerHTML)" end end end end main.rhtml: <html> <body> <div id="stuff">Here is some stuff</div> <form target="frame" action="form_action"> <input type="file" name="uploaded_file"/> <input type="submit"/> </form> <iframe id=''frame'' name="frame"></iframe> </body> </html>
Published at http://agilewebdevelopment.com/plugins/ responds_to_parent :) -- Benjamin Curtis http://www.bencurtis.com/ http://www.tesly.com/ -- Collaborative test case management http://www.agilewebdevelopment.com/ -- Resources for the Rails community On May 29, 2006, at 9:13 AM, Sean Treadway wrote:> ** File uploads with AJAX mojo ** > > Respond with RJS to your parent window with a form action targeted > to a hidden IFRAME. Handles all the painful situations like > scoping your JavaScript to your parent window generating the script > block for execution and clearing the IFRAME after execution so the > back button doesn''t re-execute the action. > > `plugin install http://sean.treadway.info/svn/plugins/ > responds_to_parent` > > class EnterpriseElevatorPitchExampleController < > ActionController::Base > def iframe_target_action > responds_to_parent do > render :update do |page| > page.hide :progress_bar > page.show :success > page.replace_html :success, ''Finished'' > page.alert "RJS isn''t limited to XMLHttpRequest" > end > end > end > end > > Discuss, comment, and feedback for improvement: > http://sean.treadway.info/articles/2006/05/29/iframe-remoting-made- > easy > > RespondsToParent > ===============> > Adds responds_to_parent to your controller to respond to the parent > document of your page. > Make Ajaxy file uploads by posting the form to a hidden iframe, and > respond with > RJS to the parent window. > > Example > ======> > Controller: > class Test < ActionController::Base > def main > end > > def form_action > # Do stuff with params[:uploaded_file] > > responds_to_parent do > render :update do |page| > page << "alert($(''stuff'').innerHTML)" > end > end > end > end > > main.rhtml: > <html> > <body> > <div id="stuff">Here is some stuff</div> > <form target="frame" action="form_action"> > <input type="file" name="uploaded_file"/> > <input type="submit"/> > </form> > <iframe id=''frame'' name="frame"></iframe> > </body> > </html> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Sean Treadway wrote:> ** File uploads with AJAX mojo ** > > Respond with RJS to your parent window with a form action targeted to > a hidden IFRAME. Handles all the painful situations like scoping > your JavaScript to your parent window generating the script block for > execution and clearing the IFRAME after execution so the back button > doesn''t re-execute the action. > > `plugin install http://sean.treadway.info/svn/plugins/ > responds_to_parent` > > class EnterpriseElevatorPitchExampleController < ActionController::Base > def iframe_target_action > responds_to_parent do > render :update do |page| > page.hide :progress_bar > page.show :success > page.replace_html :success, ''Finished'' > page.alert "RJS isn''t limited to XMLHttpRequest" > end > end > end > end > > Discuss, comment, and feedback for improvement: > http://sean.treadway.info/articles/2006/05/29/iframe-remoting-made-easy > > RespondsToParent > ===============> > Adds responds_to_parent to your controller to respond to the parent > document of your page. > Make Ajaxy file uploads by posting the form to a hidden iframe, and > respond with > RJS to the parent window. > > Example > ======> > Controller: > class Test < ActionController::Base > def main > end > > def form_action > # Do stuff with params[:uploaded_file] > > responds_to_parent do > render :update do |page| > page << "alert($(''stuff'').innerHTML)" > end > end > end > end > > main.rhtml: > <html> > <body> > <div id="stuff">Here is some stuff</div> > <form target="frame" action="form_action"> > <input type="file" name="uploaded_file"/> > <input type="submit"/> > </form> > <iframe id=''frame'' name="frame"></iframe> > </body> > </html>Sean, Thanks for this excellent solution. I''ve successfully deployed this with my app. But there is one small annoyance that I''ve encountered and was wondering if others are experiencing the same: I''m running Firefox on Mac (and linux) with Firebug installed. After I upload an image, I get the following javascript error in Firebug: #-- loc is not defined [Break on this error] with(window.parent) { setTimeout(function() { window.eval(''try {\nElemen... #-- I know it''s small, and does come with any side-effects, but still, it is annoying. Any ideas? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Elliott, I had the same firebug error. For me the issue was that my response RJS was replacing the iframe I had posted to. Maybe you are having the same problem? I am happy to help if you want to post your code or something... Thanks -Jon -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---