本文介绍了jQuery文件上传在Rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在通过 http://railscasts观看有关Jquery文件上传的视频教程. .com/episodes/381-jquery-file-upload 并被卡住.上传图像后,我的渲染局部图像不会刷新,它会在已附加的图像下添加新图像和所有其他图像.因此,如果我将两个图像添加到新项目中,则它会显示第一个图像,然后再次使用第一个图像和第二个图像在第一个图像下方渲染部分图像,因此总共有3张照片.

Hi everyone i am following the video tutorial for Jquery File Upload at http://railscasts.com/episodes/381-jquery-file-upload and am stuck. I after upload images my render partial does not refresh, it adds the new image and all other images under the image that was already attached. So if I add two images to a new item it shows the first image, then renders the partial again with the first image and the second image under the first so there are 3 photos in total.

这是我的create.js.erb

Here is my create.js.erb

<% if @photo.new_record? %>
  alert("Failed to upload photo: <%= j @photo.errors.full_messages.join(', ').html_safe %>");
<% else %>
  $("#photos").append("<%=j render @photo %>");
<% end %>

这是我显示页面上的渲染.

Here is my render from my show page.

<div id="photos">
    <%= render 'photos/photo' %>
</div>

这是我的/photos/_photos.html.erb部分

Here is my /photos/_photos.html.erb partial

<h2>Images</h2>
<div class="photo">
<% @rental.photos.each do |photo| %>
  <p>
    <strong>Photo:</strong>
    <%= image_tag photo.image_url.to_s %>
    </script>   
  </p>
 <% end %>
</div>

这是我的照片控制器创建的

Here is my Photos Controller create

 def create
    @rental = Rental.find(params[:rental_id])
    @photo = @rental.photos.create(params[:photo].permit(:image))

    respond_to do |format|
      format.js
    end
  end

这是我的photos.js.jcoffee

Here is my photos.js.jcoffee

jQuery ->
  $('#new_photo').fileupload(replaceFileInput: false,
    paramName: 'photo[image]')


    dataType: "script"
    add: (e, data) ->
      types = /(\.|\/)(gif|jpe?g|png)$/i
      file = data.files[0]
      if types.test(file.type) || types.test(file.name)
        data.context = $(tmpl("template-upload", file))
        $('#new_photo').append(data.context)
        data.submit()
      else
        alert("#{file.name} is not a gif, jpeg, or png image file")
    progress: (e, data) ->
      if data.context
        progress = parseInt(data.loaded / data.total * 100, 10)
        data.context.find('.bar').css('width', progress + '%')

任何帮助都会很棒,我被困住了.谢谢!

Any help would be great, I'm stuck. Thanks!

非常感谢Rich Peck,他的回答完全解决了我的问题.我不得不改变

Thank you so much Rich Peck his answer fully solved my problem. I had to change

<% if @photo.new_record? %>
  alert("Failed to upload photo: <%= j @photo.errors.full_messages.join(', ').html_safe %>");
<% else %>
  $("#photos").append("<%=j render @photo %>");
<% end %>

收件人

<% if @photo.new_record? %>
  alert("Failed to upload photo: <%= j @photo.errors.full_messages.join(', ').html_safe %>");
<% else %>
  $("#photos").html("<%=j render @photo %>");
<% end %>

推荐答案

def create
    @rental = Rental.find(params[:rental_id])
    @photo = @rental.photos.create(params[:photo].permit(:image))

    respond_to do |format|
        format.js
    end
end

那是我第一次看

此修补程序实际上是使用$('#photos').html()来替换容器的. OP最初使用.append只是将内容追加到容器中,这根本不能很好地工作

The fix was actually to use $('#photos').html() to replace the container. The OP was originally using .append to just append content to the container, which didn't work very well at all

这篇关于jQuery文件上传在Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 13:45