jquery form plugin / rails file upload / plain text respond instead of html
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:
<div class="report round_corners box_shadow"><h5>bearbeitet am Freitag, 04. Juni 2010, 12:06 Uhr</h5> <div class='text-right'> per Telefon am 05.07.2010 </div> <p class='report_content'> fg </p> <div class='text-right reminder'> Wiedervorlage am 14.12.2015 <br /> <a href="/visitreports/138/edit">Bericht bearbeiten >></a> </div> <h5>Dateien</h5> <a href="/assets/27">bg_mainmenu.gif</a> </div>
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
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
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
精彩评论