开发者

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:

&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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜