1

i'm trying to get a file upload via ajax form to work.. as i am using the jquery form plugin the file upload works well.. but the rendered html in the respond_to js block is returned as plain text. hope someone can help

controller:

if @visitreport.save
flash[:notice] = "Der Besuchsbericht wurde erstellt."
respond_to do |format|
format.html{redirect_to @visitreport}
format.js
end

create.js.erb:

$("#last_customers").replaceWith('<div id="last_customers"><%= escape_javascript(render :partial => 'customers/last_customers') %> </div>');
$("#reminder").replaceWith('<div id="reminder"><%= escape_javascript(render :partial => "customers/reminder", :locals => {:date => Date.today+1}) %>');
.. and some other jquery - magic

_last_customers.html.haml: -- just for example

%h5 Die zuletzt bearbeiteten Kunden
%hr
.subcolumns
-get_user_customers.each do |uc|
.c25l
.subcl
=link_to("#{uc.id}", customer_path(uc.id))
.c75l
.subcl
=link_to("#{get_customer_name(uc.id)}", customer_path(uc.id))

the response looks like this:

&lt;div class="report round_corners box_shadow"&gt;&lt;h5&gt;bearbeitet am Freitag, 04. Juni 2010, 12:06 Uhr&lt;/h5&gt; &lt;div class='text-right'&gt; per Telefon am 05.07.2010 &lt;/div&gt; &lt;p class='report_content'&gt; fg &lt;/p&gt; &lt;div class='text-right reminder'&gt; Wiedervorlage am 14.12.2015 &lt;br /&gt; &lt;a href="/visitreports/138/edit"&gt;Bericht bearbeiten &gt;&gt;&lt;/a&gt; &lt;/div&gt; &lt;h5&gt;Dateien&lt;/h5&gt; &lt;a href="/assets/27"&gt;bg_mainmenu.gif&lt;/a&gt; &lt;/div&gt;

application.js:

("#new_visitreport").submit(function() {
$(this).ajaxSubmit(
dataType  : 'script',
  iframe    : true,
success   : function (data) {
    data = eval(data.replace("<pre>", "").replace("</pre>", ""));
  }
});
return false;
});

what am i doing wrong? hope somebody can help! jquery version 1.4.2

2 Answers 2

1

According to the jQuery Form documentation, it is recommended to wrap the response in textarea. Example in erb:

<textarea>
  jQuery("#documents").prepend("<%= escape_javascript(render @document) %>");
</textarea>

Then set the content type to text/html on your controller:

respond_to do |format|
  # jQuery Form needs to have text/html as Content-Type.
  format.js { render :content_type => Mime::HTML.to_s }
end
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! This solution works quite well for me .. It seems i've overlooked the the textarea part in the jquery form doc.
@ThiefMaster The documentation explains it. It has to do with the hidden iframe technique used to upload the file. The server response is written to the hidden iframe. It's fine if the response is HTML or XML, not if it's JavaScript or JSON because some characters will then have to be represented in entity codes. So as a workaround, the JavaScript and JSON codes can be wrapped in textarea.
Yes, I fully understand that. However, text/plain wouldn't have any problems with this. Anyway, I read that IE sometimes inspects the contents for text/plain contents and thus it won't work.
Ah, sorry, I misunderstood your question. Must be the late night drowsiness :) I tested with text/plain, but it doesn't work at least with Firefox 5. It says syntax error &lt;textarea&gt;. Removing the textarea tag gives different kind of errors, as if the JS is not properly escaped.
0

If you're using Rails 3 and you'd like to simplify the process a bit, you should try out the Remotipart gem:

http://rubygems.org/gems/remotipart

http://github.com/leppert/remotipart

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.