undefined method `signed_id' for nil:NilClass

不羁的心 提交于 2019-12-23 03:03:08

问题


A while back I tried to delete attachments of the active storage so I follow the answer of this question: Rails 5.2 Active Storage purging/deleting attachements

But after doing this:

def delete_image_attachment
  @image = ActiveStorage::Blob.find_signed(params[:id])
  @image.purge
  redirect_to collections_url
end 

I start getting this error every time I entered a view with images or a link to the destroy method:

So I change the code a bit:

routes.rb

match 'vehicles/:id/:image_id' => 'vehicles#delete_image_attachment', :via => :delete, :as => :delete_image_attachment

vehicles_controller.rb

def delete_image_attachment
  @image = ActiveStorage::Attachment.find(params[:image_id])
  @image.purge
  redirect_back(fallback_location: vehicles_path)
end

in a view that I want to delete some image

<% @vehicle.images.each do |img| %>
  <%= image_tag img %>
  <%= link_to delete_image_attachment_url(@vehicle, img), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>

That kind of works for the vehicles that already existed, but when I create a new vehicle the same error appears in every view with an image of that vehicle.

Does anyone knows what kind of error is this, and how can I fix it? Also, there is any way to delete all data from active storage, like resetting the table? Thank you!

来源:https://stackoverflow.com/questions/51170577/undefined-method-signed-id-for-nilnilclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!