嗨,我目前正在制作一个包含用户、相册和照片的应用程序。当我上传图片时,我设法让 AJAX 为创建操作工作,但我无法让 AJAX 为删除工作。相反,它会将我重定向到图片的空白显示页面。谁能给我一些指导?
照片控制器:
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 部分:
<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>
最后是最初发生删除的 albums/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>