Rails 4.2.0 - Errno::EACCES (Permission denied # dir_s_mkdir - /files) - ruby-on-rails

In rails 4.2.0, I am using paperclip for file uploads. But it is throwing an error like Errno::EACCES (Permission denied # dir_s_mkdir - /files), how can I fix this issue?
When I run gem list paperclip, I got the list like below
paperclip (4.3.0, 4.2.2, 4.2.0, 2.4.5)
In controller, I have tried 2 ways, one is #file = Asset.new(:document=>params[:asset][:document]) and the other way is
#file = Asset.new(user_params)
def user_params
params.require(:asset).permit(:document)
end
In model,
attr_accessible :status, :document_file_name, :document_content_type, :document_file_size
attr_accessible :document
has_attached_file :document,
:url => '/files/:assetable_id/:basename.:extension',
:path => "/files/:assetable_id/:basename.:extension",
:storage => :filesystem
How can I solve this permission denied issue?

Change your path to the following (using :rails_root):
:path => ":rails_root/files/:assetable_id/:basename.:extension"
rails_root will give you the path to your app.

To create Directory on local drive here is the running code-
To do so I was executing-
Dir.mkdir(Rails.root+ '/' + 'export')
But getting error as-
Errno::EACCES: Permission denied # dir_s_mkdir - /Main_File
I know what was the reason, it was looking for Super User ($ sudo) permission but we can't provide machine password each time.
Following worked for me as required-
Dir.mkdir(File.join(Dir.home, ".foo"), 0700) #=> 0
To create and store the path in a variable-
main_file = File.exist?( File.join(Dir.home, "Main_File") ) ? File.join(Dir.home, "/Main_File") : Dir.mkdir( File.join(Dir.home, "Main_File") )
Above will create file if doesn't exist
If it exist will access that and store in variable main_file.
Thanks for this link!
Hope will work for you !

Related

ENOENT Error in Serializer

I have just started working with rails and I encountered this error which does not give a lot of detail. Since I am not familiar with ruby on rails, perhaps someone here can help.
The error occurs in the active model serializer for the model.
class SecuritySerializer < ActiveModel::Serializer
attributes :id, :name, :ticker, :identifier, :weight
end
the rendering occurs as follows:
def index
#securities = Security.all
render(json: #securities, each_serializer: SecuritySerializer)
end
The error that I get:
Errno::ENOENT (No such file or directory # rb_sysopen - C):
app/serializers/security_serializer.rb:1:in `<top (required)>'
app/controllers/securities_controller.rb:9:in `index'
EDIT
I am using 64-bit ruby on windows 8.
I added this to a file called serializer_init.rb in config/initializers
ActiveModel::Serializer.config.adapter = :json_api
I was using version 0.10.0. I downgraded to 0.8.0 and removed the initializer. This solved the issue.
User gem from branch master.
gem 'active_model_serializers', :git => 'git://github.com/rails-api/active_model_serializers.git'

Rails Paperclip Processor: shell command failing

I've got a problem with a custom paperclip processor within my rails app. I upload a soundfile and the processor creates an waveform image via a shell command (provided by this gem)
I'm running RoR 3.2.7 / Ruby 1.9.3 on Ubuntu 12.04 (production environment). My model with paperclip attachment looks like the following:
# encoding: utf-8
class Track < ActiveRecord::Base
has_attached_file :original_file,
:styles => { :waveform_image => { :waveform => true } },
:processors => [:sound_processor],
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:url => ":s3_eu_url",
:path => "sounds/:id/:five_digit_id_:basename_:style.:extension"
end
The corresponding Paperclip Processor:
class Paperclip::SoundProcessor < Paperclip::Processor
def initialize file, options = {}, attachment = nil
# cut for brevity
end
def make
src = #file
dst = Tempfile.new([#basename, #current_format])
dst.binmode
if #waveform
cmd = "waveform #{File.expand_path(src.path)} #{File.expand_path(dst.path)}"
Paperclip.log(cmd)
out = `#{cmd}`
dst = File.open "#{File.expand_path(dst.path)}"
end
dst
end
end
When the command
waveform #{File.expand_path(src.path)} #{File.expand_path(dst.path)}
is getting executed on the production machine (Ubuntu 12.04), the following error comes up:
/usr/bin/env: ruby: No such file or directory
However the usr/bin/env is a file instead of an directory. Since there is no ruby executable, how can I pass the right location when executing the shell command?
PS: On my development machine (OSX), on usr/bin/env is a copy of my rails app directory. It's working like a charm in development. I appreciate your help!
The error message is indicating that the ruby executable was not found. It is not related to the code that you have included in the question as of now.
The error is coming from the script file that includes the waveform #{File.expand_path(src.path)} #{File.expand_path(dst.path)} line.
Check out these answers to the issue:
- https://stackoverflow.com/a/6126419/429758
- https://stackoverflow.com/a/5241638/429758
- https://stackoverflow.com/a/5241593/429758
- https://stackoverflow.com/a/1064319/429758
They should give you some idea as to why the ruby executable is not being found on your production environment.

Paperclip Errno::EACCES (Permission denied - /system)

My production environment is : ruby 1.9.2-p320 , rails 3.2.7, paperclip 3.1.4, mysql, Ubuntu 8.10 x86 64bit.
I have a Errno:EACCES Permission denied /system error when i try to upload a file with paperclip. Useless to say that locally this doesn't happen.
I checked the public directory permissions and it's 775, the public/system permission is 777 as well as all it's inner directory. The tmp directory permission is : 775 too.
Moreover the user used to deploy the application is www-data:root
The model's attachment is set like this :
has_attached_file :fichier,
:path => "/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
I can't find out why i get this error. Anyone has got an idea ?
Thanks
Your code DOES NOT try to save the uploaded file in:
/path/to/app/public/system/:attachment/:id/:style/:filename
but in:
/system/:attachment/:id/:style/:filename
Try this instead:
has_attached_file :fichier,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"

net-sftp ruby, uploading file error

I have a rails 3.2.2 app that's got the net-sftp gem installed on it.
I've created a simple controller to test the most basic feature of uploading a file to remote sftp.
This is my controller:
class UploadsController < ApplicationController
require 'net/sftp'
def upload
Net::SFTP.start('host', 'root', :password => 'password') do |sftp|
#sftp = sftp # I've got a session object so that seems to work
# upload a file or directory to the remote host
sftp.upload!("/Users/kensodev/Desktop/ashrit.xml", "/domains/inbar-paz.com/html/test/ashrit.xml")
end
end
end
When I visit "localhost:3000/uploads/upload" path I get this error:
Net::SFTP::StatusException open /domains/inbar-paz.com/html/test/ashrit.xml (2, "no such file")
Maybe I got the paths wrong?
Thanks you for trying to help :)
Paz.
One of the directories in this path "/domains/inbar-paz.com/html/test/" doesn't not exist.

Rails 3 app + paperclip gem + production mode = download of empty files

I've installed the paperclip gem for a Rails 3 application. Everything works fine in development mode. However, when running in production mode, if I upload a file and then try to download it again, it downloads a file with the correct name and extension, but it is an empty file. When looking on the server, the file does get uploaded and is in the correct directory. (I have an "uploads" folder in my application root.)
Anyone had this happen?
My model:
# app/models/document.rb
class Document < ActiveRecord::Base
belongs_to :kase
has_attached_file :document, :path => (Rails.root + "uploads/:class/:kase_id/:id").to_s, :url => ":class/:id"
validates_attachment_presence :document
validates_attachment_content_type :document, :content_type => [
'application/pdf',
'image/png',
'image/jpeg',
'image/pjpeg',
'text/plain'
]
end
My controller:
# app/controllers/documents_controller.rb
class DocumentsController < ApplicationController
respond_to :html
before_filter :initialize_kase # Sets the #kase instance
def show
#document = #kase.documents.find(params[:id])
send_file #document.document.path, :filename => #document.document_file_name, :content_type => #document.document_content_type
end
end
And my initializer (setting the :kase_id placeholder used in has_attached_file above:
# config/initializers/paperclip.rb
Paperclip.interpolates('kase_id') do |attachment, style|
"kases/#{attachment.instance.kase.id.to_s}"
end
I should probably mention, too, that I am accessing this as a nested controller (/kases/XX/documents/XX). Not sure if that has an effect or not...
If you are using Apache and Passenger, (possibly other servers as well) and have the line:
config.action_dispatch.x_sendfile_header = "X-Sendfile"
in your production.rb env file, then you have two options:
Install the apache module mod-xsendfile
Comment out that line and let Rails send the files instead of Apache, like it does in development mode.
Are you carrying over the uploads directory each time you deploy your app to production? Assuming that you're using capistrano (or similar) for deployment, each time you deploy you might be creating a new uploads directory in the newly-deployed release directory. In that case, the previously-uploaded files are residing in older deployed releases (if you didn't delete those) and would no longer be accessible to your app.
You want to create e.g. shared/uploads directory that is symlinked into your app on each deploy.

Resources