简体   繁体   English

使用remotipart插件上传图像时,T​​extarea包裹着JS

[英]Textarea wrapped around JS when uploading images with remotipart plugin

I am using remotipart plugin to do the Ajax image upload. 我正在使用remotipart插件来执行Ajax图像上传。 Recently I upgraded the Rails version from 3.1.10 to 3.2.13. 最近我将Rails版本从3.1.10升级到3.2.13。 After upgrading rails version I found that ajax image upload is not working. 升级rails版本后,我发现ajax图像上传无效。

I have form which submits the image using remotipart plugin and renders the response.But after rails upgrade I am not able to render the response. 我有使用remotipart插件提交图像的表单并呈现响应。但是在rails升级之后我无法呈现响应。 While investigating the issue I found that response is wrapped with textarea block and thus not getting rendered. 在调查问题时,我发现响应是用textarea块包装的,因此不会被渲染。

I referred some of the related stackoverflow questions as well as issues reported on github by remotipart users but not able to figure out any resolution for this issue. 我提到了一些相关的stackoverflow问题以及remotipart用户在github上报告的问题,但无法找出解决此问题的任何方法。

viz: ajax post request responds with an html element when including an attachment with paperclip 即: 当使用回形针包含附件时,ajax post请求使用html元素进行响应

Trouble with Paperclip and Ajax with Rails 3.2.8 Paperclip和Ajax与Rails 3.2.8的问题

https://github.com/JangoSteve/remotipart/issues/89 https://github.com/JangoSteve/remotipart/issues/89

Would anyone please suggest how I can resolve this issue or what I am doing wrong? 有人请建议我如何解决这个问题或我做错了什么?

Thanks and Regards, 谢谢并恭祝安康,

You can do this on your js.erb callback file: 您可以在js.erb回调文件中执行此操作:

<% if remotipart_submitted? %>
   //this will be executed when a file is uploaded
   $("#div").html("<%= escape_javascript( render "form" ).gsub('&quot;', "'") %>");
<% else %>
   $("#div").html("<%= escape_javascript( render "form" ) %>");
<% end %>
   //Here you can put more js code

I've used an example code since you did't post your own. 我使用了一个示例代码,因为你没有发布自己的代码。 With this you don't need to downgrade your remotipart. 有了这个,你不需要降级你的remotipart。 Found this solution at https://github.com/JangoSteve/remotipart/issues/89 . https://github.com/JangoSteve/remotipart/issues/89上找到了这个解决方案。 Worked for me. 为我工作。

For me the only solution for this problem was to write custom form submission function, which sends Ajax request with form data. 对我来说,这个问题的唯一解决方案是编写自定义表单提交功能,该功能使用表单数据发送Ajax请求。 This is not the best solution because FormData is only supported by new browsers (IE10+). 这不是最佳解决方案,因为FormData仅受新浏览器(IE10 +)支持。

Here is the coffescript: 这是coffescript:

formAjaxSubmit = ($form, $url) ->
  $formData = new FormData($form[0])

  $.ajax(
    type: 'POST'
    url: $url
    data: $formData
    dataType: 'JSON'
    processData: false
    contentType: false # this is crucial line for $.ajax to work with formData
    error: (xhr, status, error) ->
      $.fn.process_ajax_error(null, xhr, status, error)
    success: (data, status, xhr) ->
      $.fn.process_ajax_success(null, data, status, xhr)
  )
  return false

Arguments: 参数:

  • $form - jQuery form object like $('#myForm') $ form - jQuery表单对象,如$('#myForm')
  • $url - sumbission url, usually it should has the form action value $('#myForm').attr('action') $ url - sumbission url,通常它应该具有表单操作值$('#myForm').attr('action')

$.fn.process_ajax_... - these are the custom response handling functions $.fn.process_ajax_... - 这些是自定义响应处理函数

Hope you have found the solution. 希望你找到解决方案。 putting data: {type: :script} in the form should work fine 在表单中放置data: {type: :script}应该可以正常工作

This is what finally fixed the issue for me. 这就是最终为我解决的问题。 also from here: https://github.com/JangoSteve/remotipart/issues/89 也可以从这里: https//github.com/JangoSteve/remotipart/issues/89

The previous similar suggestion with gsub was generating some invalid json/js for me. 以前与gsub类似的建议为我生成了一些无效的json / js。 This solution below worked! 以下解决方案有效!

<% if remotipart_submitted? %>
 $('.form-wrapper').html("<%= j "#{render('form')}" %>");
<% else %>
 $('.form-wrapper').html("<%= j render('form') %>");
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM