Active Storage 清除方法为 NilClass 抛出未定义的方法 signed_id
Active Storage purge method throws undefined method signed_id for NilClass
我遇到的问题与此处的问题类似,但删除数据库对我的项目来说不是一个可行的解决方案:Rails 5.2 ActiveStorage undefined method `signed_id' for nil:NilClass
我有一个表单,允许通过单击 link 上传新图像和删除已上传的图像。该表格在 Rails Active Storage 和 S3 上使用 Ruby。我按照答案 创建了 link 用户可以单击它来删除图像。我认为它正在删除图像。但是,当页面重新加载时,我收到此错误
错误:无法将图像解析为 URL:nil:NilClass
的未定义方法“signed_id”
单击 "Remove" link 后,页面应重新加载并显示剩余图像。我该如何解决这个错误?
这是模型
class Space < ApplicationRecord
belongs_to :user
has_many_attached :space_image
end
控制器
class SpacesController < ApplicationController
before_action :set_space, except: [:index, :new, :create, :delete_image_attachment]
before_action :authenticate_user!, except: [:show]
def delete_image_attachment
@space_image = ActiveStorage::Blob.find_signed(params[:id])
@space_image.purge_later
redirect_back(fallback_location: spaces_path)
end
private
def set_space
@space = Space.find(params[:id])
end
end
风景
<div>
<% if @space.space_image.attached? %>
<% @space.space_image.each do |space_image| %>
<%= image_tag space_image, class: "avatar-medium" %>
<%= link_to 'Remove', delete_image_attachment_space_url(space_image.signed_id),
method: :delete,
data: { confirm: 'Are you sure?' } %>
<% end %>
<% end %>
</div>
** 服务器错误输出**
ActionView::Template::Error (Can't resolve image into URL: undefined method `signed_id' for nil:NilClass):
1: <div>
2: <% if @space.space_image.attached? %>
3: <% @space.space_image.each do |space_image| %>
4: <%= image_tag space_image, class: "avatar-medium" %>
5: <%= link_to 'Remove', delete_image_attachment_space_url(space_image.signed_id),
6: method: :delete,
7: data: { confirm: 'Are you sure?' } %>
app/views/spaces/_spaceimg.html.erb:4:in `block in _app_views_spaces__spaceimg_html_erb___4211978368865358240_70218939472720'
app/views/spaces/_spaceimg.html.erb:3:in `_app_views_spaces__spaceimg_html_erb___4211978368865358240_70218939472720'
app/views/spaces/listing.html.erb:121:in `block in _app_views_spaces_listing_html_erb__2591928334825339114_70218934956920'
app/views/spaces/listing.html.erb:15:in `_app_views_spaces_listing_html_erb__2591928334825339114_70218934956920'
发生这种情况是因为您直接从ActiveStorage::Blob中删除了一条记录,这样就破坏了一致性。
相反,您必须在 ActiveStorage::Attachment then delete it: see this line 中找到相关记录。
编辑:如何删除附件的一个示例。
已在控制台中测试,因此您可能需要修正一些语法。
space_image.id
应该returnActiveStorage::Attachment中记录的id,所以传给controller:
delete_image_attachment_space_url(space_image.id)
然后在controller中,找到记录并应用你需要删除它的方法:
def delete_image_attachment
@space_image = ActiveStorage::Attachment.find(params[:id])
@space_image.purge
# whatever
end
我遇到的问题与此处的问题类似,但删除数据库对我的项目来说不是一个可行的解决方案:Rails 5.2 ActiveStorage undefined method `signed_id' for nil:NilClass
我有一个表单,允许通过单击 link 上传新图像和删除已上传的图像。该表格在 Rails Active Storage 和 S3 上使用 Ruby。我按照答案
单击 "Remove" link 后,页面应重新加载并显示剩余图像。我该如何解决这个错误?
这是模型
class Space < ApplicationRecord
belongs_to :user
has_many_attached :space_image
end
控制器
class SpacesController < ApplicationController
before_action :set_space, except: [:index, :new, :create, :delete_image_attachment]
before_action :authenticate_user!, except: [:show]
def delete_image_attachment
@space_image = ActiveStorage::Blob.find_signed(params[:id])
@space_image.purge_later
redirect_back(fallback_location: spaces_path)
end
private
def set_space
@space = Space.find(params[:id])
end
end
风景
<div>
<% if @space.space_image.attached? %>
<% @space.space_image.each do |space_image| %>
<%= image_tag space_image, class: "avatar-medium" %>
<%= link_to 'Remove', delete_image_attachment_space_url(space_image.signed_id),
method: :delete,
data: { confirm: 'Are you sure?' } %>
<% end %>
<% end %>
</div>
** 服务器错误输出**
ActionView::Template::Error (Can't resolve image into URL: undefined method `signed_id' for nil:NilClass):
1: <div>
2: <% if @space.space_image.attached? %>
3: <% @space.space_image.each do |space_image| %>
4: <%= image_tag space_image, class: "avatar-medium" %>
5: <%= link_to 'Remove', delete_image_attachment_space_url(space_image.signed_id),
6: method: :delete,
7: data: { confirm: 'Are you sure?' } %>
app/views/spaces/_spaceimg.html.erb:4:in `block in _app_views_spaces__spaceimg_html_erb___4211978368865358240_70218939472720'
app/views/spaces/_spaceimg.html.erb:3:in `_app_views_spaces__spaceimg_html_erb___4211978368865358240_70218939472720'
app/views/spaces/listing.html.erb:121:in `block in _app_views_spaces_listing_html_erb__2591928334825339114_70218934956920'
app/views/spaces/listing.html.erb:15:in `_app_views_spaces_listing_html_erb__2591928334825339114_70218934956920'
发生这种情况是因为您直接从ActiveStorage::Blob中删除了一条记录,这样就破坏了一致性。
相反,您必须在 ActiveStorage::Attachment then delete it: see this line 中找到相关记录。
编辑:如何删除附件的一个示例。
已在控制台中测试,因此您可能需要修正一些语法。
space_image.id
应该returnActiveStorage::Attachment中记录的id,所以传给controller:
delete_image_attachment_space_url(space_image.id)
然后在controller中,找到记录并应用你需要删除它的方法:
def delete_image_attachment
@space_image = ActiveStorage::Attachment.find(params[:id])
@space_image.purge
# whatever
end