Rails - 远程销毁方法未呈现部分视图:NoMethodError 未定义照片的“id”方法
Rails - remote destroy method not rendering view partial : NoMethodError undefined method `id' for Photo
尝试远程删除视图中的图像时出现以下错误:
NoMethodError (undefined method `id' for #<Photo::ActiveRecord_Associations_CollectionProxy:0x007fa826898200>):
app/views/photos/destroy.js.erb:1:in `_app_views_photos_destroy_js_erb__997641922821004789_70180135669200'
app/controllers/photos_controller.rb:31:in `destroy'
以下是我的destroy
方法:
def destroy
@photo_destroy = Photo.find_by_id(params[:id])
@item = Item.find(@photo_destroy.item_id)
if @photo_destroy.present?
@photo_destroy.destroy
end
flash[:success] = "Photo deleted"
@photo = @item.photos
respond_to do |format|
format.html { redirect_to edit_photos_url(@item)}
format.js
end
end
而我的毁灭javascript:
$("#photo-<%= @photo.id %>").html("<%= escape_javascript(render('photos/showpics')) %>");
部分视图:
<div class="row">
<% @photo.each do |photo| %>
<li id="photo-<%= photo.id %>">
<div class="col-sm-6 col-md-4">
<div class="thumbnail" >
<%= image_tag photo.image_url(:thumb) if photo.image? %>
<div class="caption">
<%= photo.title %>
<p><a href="#" class="btn btn-primary" role="button"><%= link_to 'Remove', delete_photo_path(photo), method: :delete, remote: true %></a></p>
</div>
</div>
</div>
</li>
<% end %>
</div>
在我看来,部分视图中的 @photo
由于缺少更好的术语而未与 destroy
方法中的 destroy 链接。现在我考虑一下,可以将方法中的 @photo
传递给 javascript 吗?或者最好的方法是什么?
根据评论更新。
看看你做了什么:
您的 @photo
是 Photo::ActiveRecord_Associations_CollectionProxy 并且您正在调用您分配的位置 @item.photos
。
为了获得您的部分作品,您需要获得已删除对象的 ID。这是:
$("#photo-<%= @photo_destroy.id %>").html("<%= j render 'photos/showpics', {photos: @photo} ) %>");
您还需要更新您的部分文件:
<div class="row">
<% photos.each do |photo| %> # look at that photos, it comes from the local when we render it in j render 'photos/showpics', {photos: @photo}
<li id="photo-<%= photo.id %>">
....
这应该有效!
我最后做的是在 showpics
部分中为 row
class 分配一个 ID:
<div class="row" id="photo-row">
正在更改 javascript:
$("#photo-row").html("<%= escape_javascript(render('photos/showpics')) %>");
现在,当其中一张照片被删除时,所有照片都会正确更新。感谢大家的帮助。
尝试远程删除视图中的图像时出现以下错误:
NoMethodError (undefined method `id' for #<Photo::ActiveRecord_Associations_CollectionProxy:0x007fa826898200>):
app/views/photos/destroy.js.erb:1:in `_app_views_photos_destroy_js_erb__997641922821004789_70180135669200'
app/controllers/photos_controller.rb:31:in `destroy'
以下是我的destroy
方法:
def destroy
@photo_destroy = Photo.find_by_id(params[:id])
@item = Item.find(@photo_destroy.item_id)
if @photo_destroy.present?
@photo_destroy.destroy
end
flash[:success] = "Photo deleted"
@photo = @item.photos
respond_to do |format|
format.html { redirect_to edit_photos_url(@item)}
format.js
end
end
而我的毁灭javascript:
$("#photo-<%= @photo.id %>").html("<%= escape_javascript(render('photos/showpics')) %>");
部分视图:
<div class="row">
<% @photo.each do |photo| %>
<li id="photo-<%= photo.id %>">
<div class="col-sm-6 col-md-4">
<div class="thumbnail" >
<%= image_tag photo.image_url(:thumb) if photo.image? %>
<div class="caption">
<%= photo.title %>
<p><a href="#" class="btn btn-primary" role="button"><%= link_to 'Remove', delete_photo_path(photo), method: :delete, remote: true %></a></p>
</div>
</div>
</div>
</li>
<% end %>
</div>
在我看来,部分视图中的 @photo
由于缺少更好的术语而未与 destroy
方法中的 destroy 链接。现在我考虑一下,可以将方法中的 @photo
传递给 javascript 吗?或者最好的方法是什么?
根据评论更新。
看看你做了什么:
您的 @photo
是 Photo::ActiveRecord_Associations_CollectionProxy 并且您正在调用您分配的位置 @item.photos
。
为了获得您的部分作品,您需要获得已删除对象的 ID。这是:
$("#photo-<%= @photo_destroy.id %>").html("<%= j render 'photos/showpics', {photos: @photo} ) %>");
您还需要更新您的部分文件:
<div class="row">
<% photos.each do |photo| %> # look at that photos, it comes from the local when we render it in j render 'photos/showpics', {photos: @photo}
<li id="photo-<%= photo.id %>">
....
这应该有效!
我最后做的是在 showpics
部分中为 row
class 分配一个 ID:
<div class="row" id="photo-row">
正在更改 javascript:
$("#photo-row").html("<%= escape_javascript(render('photos/showpics')) %>");
现在,当其中一张照片被删除时,所有照片都会正确更新。感谢大家的帮助。