Forum: Ruby on Rails [ANN] IFRAME based RJS - responds_to_parent

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sean Treadway (Guest)
on 2006-05-29 20:14
(Received via mailing list)
** 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/ifra...

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>
Benjamin C. (Guest)
on 2006-05-30 00:53
(Received via mailing list)
Published at http://agilewebdevelopment.com/plugins/
responds_to_parent :)

--
Benjamin C.
http://www.bencurtis.com/
http://www.tesly.com/ -- Collaborative test case management
http://www.agilewebdevelopment.com/ -- Resources for the Rails community
Elliott B. (Guest)
on 2007-07-13 19:09
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/ifra...
>
> 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?
Jon P. (Guest)
on 2008-03-09 03:40
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
This topic is locked and can not be replied to.