使用jquery删除活动存储附件并重新发送附件帮助?最佳实践是什么



我试图删除一个活动的存储附件,但真的不知道如何解决它,我遇到了很多不同的错误。

错误:

NoMethodError at /rooms/10/photos/72
====================================
> undefined method `count' for nil:NilClass
app/views/photos/_photos_list.html.erb, line 1

我的代码是:

视图:photo_upload.html.erb

<div class="col-md-offset-3 col-md-12">
<%= form_for @room do |form| %>
<div class="form-group">
<%= form.file_field :photos, multiple: true, class: 'form-control'%>
</div>
<%= form.submit 'save', class: 'form-control btn btn-success'%>
<% end %>
</div>
<div id="photos"><%= render 'photos/photos_list' %></div>

部分:照片/_photos_list.html.erb

<% if @photos.count > 0 %>
<div class="row">
<% @photos.each do |photo| %>
<div class="col-md-4">
<%= image_tag photo, width: 500, class: 'img-thumbnail' %>
<%= link_to(room_photo_path(@room.id, photo.id), remote: true, method: :delete, data: {confirm: 'are you sure'}) do %>
<i class="fa fa-trash-o" area-hidden="true"></i>
<% end %>
</div>
<% end %>
</div>
<% end %>

控制器:photos_controller.html.erb

def destroy
@photo = ActiveStorage::Attachment.find(params[:id])
@photo.purge
respond_to :js
end
end

销毁.js.erb$('#photos').html("<%= j render 'photos_list' %>")

型号:room.rb

has_many_attached :photos

问题已修复:在控制器中发现小的打字错误,应该是这样的:link_to(room_photo_path(photo.record_id, photo), remot...

最新更新