我有一个带有产品的应用程序-每个产品都有注释/常见问题/附件之类的东西。
我可以成功删除注释和常见问题解答,但不能删除Active Storage附件。
有人可以帮忙吗?我曾尝试在Products控制器中使用一种单独的方法,但是这种方法不起作用,所以我当前的代码行是使用Uploads控制器。
我当前遇到的错误是:
NameError in UploadsController#destroy
uninitialized constant Upload
上传控制器:
class UploadsController < ApplicationController
load_and_authorize_resource :nested => :product
def destroy
@product = Product.find(params[:id])
@upload = @product.ActiveStorage::Attachment.find(params[:id])
@upload.purge
redirect_back(fallback_location: products_path)
end
end
产品视图:
<% @product.uploads.each do |upload| %>
<% if can? :destroy, upload %>
<td><%= link_to t('X', :default => t("X")),
product_upload_path(@product, upload),
:method => :delete,
:data => { :confirm => t('.confirm', :default => 'Are you sure you want to delete this attachment?') },
:id =>'delete-faq' %></td>
<% end %>
<% if upload.variable? %>
<span><%= image_tag upload.variant(resize: "100x100"), class: "other-image" %></span>
<% elsif upload.previewable? %>
<span><%= link_to image_tag(upload.preview(resize: "100x100"), class: "other-image"), rails_blob_path(upload), target: "_blank" %></span>
<% else %>
<span><%= link_to image_tag("paper.jpg", size: "100x100", class: "other-image"), rails_blob_path(upload), target: "_blank" %></span>
<% end %>
<% end %>
路线:
resources :products do
resources :notes
resources :faqs
resources :uploads
end
答案 0 :(得分:0)
我知道这不是最RESTful的解决方案,但我可以使用它,因此添加此作为答案,以防其他人将头发扯掉。
我已经检查了日志,并且确实删除了附件图像和斑点。
就我而言,产品has_many个上传。
routes.rb:
resources :products do
resources :uploads do
match '/remove', to: 'products#remove', via: 'delete'
end
end
产品控制器(是的,我知道,把它放在这里并不理想)
def remove
@product = Product.find(params[:product_id])
@upload = @product.uploads.find(params[:upload_id])
@upload.purge
redirect_to product_path(@product), notice: "Upload was successfully removed."
end
产品展示视图:
<% @product.uploads.each do |upload| %>
<%= link_to "X", product_upload_remove_path(@product, upload),
:method => :delete,
:data => { :confirm => t('.confirm', :default => 'Are you sure you want to delete this upload?') },
:id =>'delete-faq', :class => 'delete' %></td>
<% end %>
希望这将对其他人有所帮助或为更RESTful解决方案奠定基础。