您好我正在制作一个包含用户,相册和照片的应用。当我上传图片时,我设法让AJAX为创建操作工作,但是我无法让AJAX为删除工作。相反,它将我重定向到pic的空显示页面。谁能给我一些指导?
照片控制器:
def destroy
@user = User.find(params[:user_id])
@album = @user.albums.find(params[:album_id])
@photo = @album.photos.find(params[:id])
# @photo.destroy
flash[:success] = "Photo successfully deleted."
respond_to do |format|
if @photo.destroy
format.js
# format.json { render json: @photo, status: :created, location: @photo}
# redirect_to user_album_path(@user, @album)
end
end
end
destroy.js.erb(我应该这样做吗?)
$('.destroypicswrapper').remove(".<%= @photo.avatar.url %>");
_photodestroy.html.erb partial:
<div class="picthumb <%= photo.avatar.url %>">
<%= link_to image_tag(photo.avatar.url ? photo.avatar.url(:small) : @album.name), root_url %>
<br>
<%= link_to "Delete", user_album_photo_path(@user, @album, photo), :remote => true, method: :delete, data: { confirm: "Are you sure? "} %>
</div>
最后是最初发生删除的专辑/ edit.html.erb页面:
<% provide(:title, "Edit album") %>
<%= render 'form' %>
<div class="destroypicswrapper">
<% if @album.photos.any? %>
<%= render :partial => 'photodestroy', :collection => @album.photos, :as => :photo %>
<% end %>
</div>
答案 0 :(得分:1)
我认为您不想将图像的完整网址呈现为div类。另外,你的Jquery是错误的。尝试这样的事情:
destroy.js.erb
$('.destroypicswrapper > #photo_<%= @photo.id %>').remove();
将您的_photodestroy.html.erb调整为:
<div class="picthumb" id="photo_<%= photo.id %>">