The app works fine in development but in production I get Errno::EACCES Permission Denied error when I try to upload a file using Carrierwave. I'm sure it has something to do with permissions. How can I set the permissions to allow file uploads?
pdf_uploader.rb
def store_dir
"#{Rails.root}/uploads/#{model.id}"
end
def cache_dir
"#{Rails.root}/tmp/uploads/cache/#{model.id}"
end
As far as I know there are two things that can be going on here:
1) The directory you're saving your images to doesn't have read/write privileges for other users.
To fix:
terminal
$ cd [my_app]
$ chmod -R 666 tmp
$ chmod -R 666 public/uploads
or if you're saving your images in an private directory:
$ chmod -R 666 private/uploads
We're using 666 over 777. 666 allows read and write privileges to a directory, and carrierwave needs to write its images. 777 allows read, write privileges and for executable files to be executed! In other words, a nasty program could be uploaded to your server disguised as an image if you're using 777. Even though carrierwave's extension white-list solves this problem, you should always use 666 over 777.
2) You're not using double quoted strings in the store_dir
method.
To fix:
app/example_uploader.rb
class BaseUploader < CarrierWave::Uploader::Base
# other methods removed for brevity
def store_dir
"#{Rails.root}/private/" # works perfectly. Many thanks to @RGB
end
end
Just want to point out how subtle this is. You need double quoted strings and Rails.root
! I was doing this:
def store_dir
Rails.root + '/private' # raises Errno::EACCES error
end
and it was not working at all. So subtle. The community should address this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With