Rails 5.2 ActiveStorage undefined method `signed_id' for nil:NilClass

末鹿安然 提交于 2019-12-02 10:12:53

问题


I implemented the code for removing images from my User model.

Rails 5.2 Active Storage purging/deleting attachements

I think that it removed the image OK, but now I'm getting an error

undefined method `signed_id' for nil:NilClass

My view (images.html.erb):

<% if @user.images.attached? %>
  <% @user.images.each do |image| %>
    <%= image_tag(url_for(image))%>
     <%= link_to 'Remove', delete_image_attachment_user_url(image.signed_id),
            method: :delete,
            data: { confirm: 'Are you sure?' } %>
  <% end %>
<% end %>

The controller

def images
end

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

I tried removing code from the view, so it just has this:

<% if @user.images.attached? %>
  <% @user.images.each do |image| %>
    <%= image_tag(url_for(image))%>
  <% end %>
<% end %>

But even after I remove the code for 'signed_id', I still receive the error. Also, I'm unsure why it looks at the line receiving the code:

<%= image_tag(url_for(image))%>

if I'm using when I have the line @user.images.attached?

I thought the error might be that there were no attached images (although there should be one attached), so I added that check.

来源:https://stackoverflow.com/questions/51085012/rails-5-2-activestorage-undefined-method-signed-id-for-nilnilclass

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