Just starting with Rails. I have a quick question that uses Paperclip and S3 for a Rails app that I am building, hosted on Heroku.
I’ve synced everything up so that attachments can be uploaded (both in production and environment) but the only problem is deleting a file from the app/S3. I’ve done a lot of searching but a lot of the implementations involve checkboxes. I’m also running everything through a Files controller to limit access to admins.
I’m using a simple Project model which has_many attachments.
When I click the delete link, I get an error from S3 saying “Error:
MethodNotAllowed. The specified method is not allowed against this resource.
“
Here’s my view:
<% @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 %>
Here’s my destroy action:
def destroy
@asset = Asset.find(params[:id])
@asset.destroy
flash[:notice] = "Attachment has been deleted."
redirect_to(:back)
end
The Project model is pretty standard:
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
What else am I missing here to get the file removed? Is it something in the model? It all worked well when I wasn’t using S3 and deleted from my SQLite or PG database.
Any help would be much appreciated and thank you!
MethodNotAllowed is an HTTP 405, which means you’re trying to do something S3 doesn’t like. I think the error is with your delete link:
Basically, you’re sending an HTTP destroy to the URL of the file on S3, which should actually go to your Asset controller, and be a
:delete.Try:
In your controller you should then handle deleting the asset:
Hope this helps!