views:

110

answers:

1

Hello, 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

A: 

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
htanata
Thank you! This solution works quite well for me .. It seems i've overlooked the the textarea part in the jquery form doc.
ThorKhan