Hi everyone, I am following a video tutorial to upload a jquery file at http://railscasts.com/episodes/381-jquery-file-upload and am stuck. After loading the images, my render is partially not updated, it adds a new image and all other images under the already attached image. Therefore, if I add two images to a new element, it displays the first image, then again displays a partial image with the first image, and the second image the first, so there are only 3 photos.
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>
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 photo controller creating
def create @rental = Rental.find(params[:rental_id]) @photo = @rental.photos.create(params[:photo].permit(:image)) respond_to do |format| format.js end end
Here are 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 wonderful, I'm stuck. Thanks!
Many thanks to Rich Peck that his answer completely 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 %>
For
<% if @photo.new_record? %> alert("Failed to upload photo: <%= j @photo.errors.full_messages.join(', ').html_safe %>"); <% else %> $("#photos").html("<%=j render @photo %>"); <% end %>