Delete active storage attachment with jquery and rerender attachments help ? and what is the best practice?

200 Views Asked by At

I'm trying to delete an active storage attachment and really don't figure out how to solve it i get many different errors.

ERRORS:

NoMethodError at /rooms/10/photos/72
====================================

> undefined method `count' for nil:NilClass

app/views/photos/_photos_list.html.erb, line 1

the code i have :

view: 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>

partial: photos/_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 %>

controller: photos_controller.html.erb

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

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

Model : room.rb

  has_many_attached :photos
1

There are 1 best solutions below

0
On

issue fixed : found small typo in controller it should be like that : link_to(room_photo_path(photo.record_id, photo), remot...