2

刚从 Rails 开始。我有一个快速的问题,它使用 Paperclip 和 S3 来构建我正在构建的 Rails 应用程序,托管在 Heroku 上。

我已经同步了所有内容,以便可以上传附件(在生产和环境中),但唯一的问题是从 app/S3 中删除文件。我做了很多搜索,但很多实现都涉及复选框。我还通过文件控制器运行所有内容以限制对管理员的访问。

我正在使用一个简单的项目模型,它有多个附件。

当我单击删除链接时,我从 S3 收到一条错误消息,提示“错误:MethodNotAllowed。指定的方法不允许针对此资源。”

这是我的看法:

<% @project.assets.each do |asset| %>
 <%= link_to File.basename(asset.asset_file_name), asset.asset.url %>
 <small>(<%= number_to_human_size(asset.asset.size) %>)</small>
 <%= link_to '[X]', asset.asset.url , confirm: 'Are you sure you want to delete this attachment?', method: :destroy %>

这是我的破坏行动:

def destroy
   @asset = Asset.find(params[:id])
   @asset.destroy
   flash[:notice] = "Attachment has been deleted."
   redirect_to(:back) 
end

Project 模型非常标准:

class Asset < ActiveRecord::Base
  # attr_accessible :title, :body

  attr_accessible :asset

  belongs_to :project
  has_attached_file :asset, :storage => :s3, :path => (Rails.root + "files/:id").to_s, :url => "/files/:id"
 end

我在这里还缺少什么来删除文件?是模型中的东西吗?当我不使用 S3 并从我的 SQLite 或 PG 数据库中删除时,一切都运行良好。

任何帮助将不胜感激,谢谢!

4

1 回答 1

2

MethodNotAllowed 是一个 HTTP 405,这意味着您正在尝试做一些 S3 不喜欢的事情。我认为错误在于您的删除链接:

<%= link_to '[X]', asset.asset.url , confirm: 'Are you sure you want to delete this attachment?', method: :destroy %>

基本上,您正在向 S3 上文件的 URL 发送 HTTP 销毁,该文件实际上应该转到您的资产控制器,并且是:delete.

尝试:

<%= link_to '[X]', asset, confirm: 'Are you sure you want to delete this attachment?', method: :delete %>

然后在您的控制器中,您应该处理删除资产:

def destroy
   @asset = Asset.find(params[:id])
   @asset.destroy
   flash[:notice] = "Attachment has been deleted."
   redirect_to(:back) 
end

希望这可以帮助!

于 2013-01-29T01:11:39.200 回答