Tuesday, June 1, 2010

Paperclip plugin Errno::EACCES Permission denied on windows

If you get the following error on windows while using paperclip plugin
[paperclip] Saving attachments.
[paperclip] deleting C:/Documents and Settings/Sandeep/My Documents/NetBeansProjects/logicare/trunk/public/system/datas/208/original/WLHBSITEAM.csv
  [4;36;1mSQL (0.0ms) [0m   [0;1mROLLBACK [0m

Errno::EACCES (Permission denied - C:/Documents and Settings/Sandeep/My Documents/NetBeansProjects/logicare/trunk/public/system/datas/208/original/WLHBSITEAM.csv):
  c:/ruby/lib/ruby/1.8/fileutils.rb:1297:in `unlink'
  c:/ruby/lib/ruby/1.8/fileutils.rb:1297:in `remove_file'
  c:/ruby/lib/ruby/1.8/fileutils.rb:1305:in `platform_support'
  c:/ruby/lib/ruby/1.8/fileutils.rb:1296:in `remove_file'
  c:/ruby/lib/ruby/1.8/fileutils.rb:771:in `remove_file'
  c:/ruby/lib/ruby/1.8/fileutils.rb:549:in `rm'
  c:/ruby/lib/ruby/1.8/fileutils.rb:548:in `each'
  c:/ruby/lib/ruby/1.8/fileutils.rb:548:in `rm'
  app/controllers/nhs_datas_controller.rb:54:in `update'
  app/controllers/nhs_datas_controller.rb:53:in `update'

Steps to fix the problem
1) create patches.rb in lib folder and add these lines
require 'tempfile'
class Tempfile
  def size
    if @tmpfile
      @tmpfile.fsync
      @tmpfile.flush
      @tmpfile.stat.size
    else
      0
    end
  end
end
2) Add require "patches.rb" in environment.rb inside Rails::Initializer.run
3)Make sure you have installed http://www.imagemagick.org/script/binary-releases.php#windows
4) Restart the server and you should be able to add, delete and update files.

 There is another possibility why you will get permission denied error. This is when u have the file related data saved in associated tables. Say users table has data_id and there is files table has the related data i.e data_file_name, data_content_type etc.
In this scenario when you try you update the file then you will get permission denied errors. One possible way to fix this issue is.
1) first upload the new file
2) Then update the related table i.e users table with data_id . Now the link to old data is lost.
3) You have the old file id as params while submit. so using the get the file and the delete it.
Sample code
@data = Data.new
    @data.data = params[:data][:data]
    @data.save
    @user = User.find_by_data_id(params[:id])
    @user.data_id = @data.id
    @user.save
    @old_data = Data.find(params[:id])
    @old_data.destroy
Let me know if there is any better way to fix this issue.

2 comments:

  1. If the file upload is done to the associated model then make sure that you have defined the association correctly

    ReplyDelete
  2. I'm getting the Errno::EACCES on Windows Vista running Rails 3.0.0, Ruby 1.9.2, and paperclip gem version 2.3.1.

    After following the steps you laid out I still have the error.

    It just shows the paperclip error with no other traceback info:
    [paperclip] There was an unexpected error while deleting directories: Errno::EACCES

    Some major head scratching here

    ReplyDelete