Take the 2-minute tour ×

I want to upload an image and show it without refresh.

The method I know is to use an hidden iframe and set the target of the form to it.

Then return a piece of javascript from controller calling "parent.func()" to do something...

But here is my problem:

It seems that the javascript will not be executed unless the foreground knows the dataType is "script", something like:

$.ajax {

but when submit the form, I have to use


So I cannot specify the dataType....

Can anyone tell me what to do?...

Or can you tell me another way to achieve my goal:

upload a image and show it without refresh in Rails 3.2.0..

share|improve this question

3 Answers 3

up vote 0 down vote accepted

try to use remotipart gem:


share|improve this answer
It works!!!!!THX!!!!!!!!!! –  HanXu Feb 1 '12 at 8:05

The way I have done it is the calling code (that calls $("...").submit()) will listen for the load event on the hidden iframe, and then react; rather than the server returning JavaScript that automatically runs.

On the iframe load event, you can turn the response into a JavaScript object if you are returning JSON:

// assuming you have a DOM element with a variable of "iframe"
var doc = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
var response = eval("(" + doc.body.innerHTML + ")");

Or, in your case if the response is just the image, then your load callback could put the response into a <div> or some other element you have already created on the page.

You may want to check out the following plugin: https://github.com/valums/ajax-upload

Or, if you want to take advantage of HTML5 features, a more advanced plugin: https://github.com/valums/file-uploader

share|improve this answer
Appreciate very much!..I will try your advice! –  HanXu Feb 1 '12 at 8:05

I think the right answer to your question is use code below

render :inline => '<script type="text/javascript">parent.callback(data)</script>'
share|improve this answer

Your Answer


