There are a number of similar questions, but none of the answers worked for me.
I'm running Rails 6.0.3.2, Ruby 2.6.6, and SQLite3 on Windows 10 version 2004 (19041.388). I followed the Getting Started guide on the official Rails site to install Ruby on Rails and and everything should be up to date.
I can delete the files normally, and I'm logged in with an administrator account — not that it should be necessary.
I'm new to Ruby and Rails, so detailed answers would be appreciated.
Code
Here's what causes the error:
def destroy
book = Book.find(params[:id])
begin
File.open(book.cover_url, 'w') do |f|
File.delete(f)
end
rescue Errno::ENOENT
end
book.destroy
redirect_to books_path
end
What this does it first delete the cover image for a book and then delete the book itself from the database.
Errors
The error screen:
If the picture doesn't load, here are the error messages:
Errno::EACCES in BooksController#destroy
Permission denied @ apply2files - D:/Projects/Web/RoR/ecommerce/app/assets/images/covers/circles_scaling_anim_positioning.png
File.delete(f)
is the culprit.
Attempted Solutions
The only actionable answer I could find for Windows was this, which advocated adding a 'lib' gem, but it did not work at all.
I also tried changing file mode from 'w' to 'wb+', but that didn't work either.
EDIT 2: As per Dave Newton's suggestion in the comments (if that's what he meant), I moved the image storage directory outside the 'app' folder; to 'public/uploads/covers'. It hasn't worked either.
EDIT 3: I copied the delete code to a new script in another directory entirely and tried it on a sample file. I got the same error. In other words, the problem is not with Rails but Ruby (or my OS).
I called
rm
on the file from the terminal and that worked just fine, so I don't know if it's a file permissions problem.
EDIT: I checked the file in question and though it still remains, it's 0 bytes large now, so I presume it was overwritten by empty data. However, the rest of the code that should've execuded on destroy
— i.e. the destruction of the object in the databse — seems not to have run, because the object is still in there.