I'm trying to register a new log
##my_logger ||= Logger.new("#{Rails.root}/log/my.log")
but when I try to generate new folders , to put it inside
##my_logger ||= Logger.new("#{Rails.root}/log/#{today.to_s}/my.log")
it returns Errno::ENOENT: No such file or directory
May it be a permission problem?
How to create a folder (if not present) with Logger.new?
Try something like this.
dir = File.dirname("#{Rails.root}/log/#{today}/my.log")
FileUtils.mkdir_p(dir) unless File.directory?(dir)
##my_logger ||= Logger.new("#{Rails.root}/log/#{today}/my.log")
You can also do this way
directory_name = "name"
Dir.mkdir(directory_name) unless File.exists?(directory_name)
Automatic creation of logging directories has been deprecated in rails. Here's a code snippet from the Logger.new code:
ActiveSupport::Deprecation.warn("Automatic directory creation for '#{log}' is deprecated. Please make sure the directory for your log file exists before creating the logger. ")
Accepted practice now is to make sure the log file (and directory) exist before creating the logger.
A way to make sure the directory exists ahead of time might be to use code similar to this:
log_file_name = '/path/to/my.log'
unless File.exist?(File.dirname(log_file_name))
FileUtils.mkdir_p(File.dirname(log_file_name))
end
Related
I have a new rails 6 application and in the lib folder I had this:
/lib/some_app_name/stripe/subscription/subscription_service.rb
module Someappname # Someappname is also in my application.rb
module Stripe
class SubscriptionService
def initialize(a)
#a = a
end
end
end
end
I then moved the 'some_app_name' folder to:
/app/some_app_name/stripe/subscription_service.rb
I read that anything inside of /app will be autoloaded and reloaded so I moved in here. It wasn't working in /lib also.
In my home_controller.rb I tried this:
ss = Someappname::Stripe::SubscriptionService.new("a")
I get an error saying:
uninitialized constant Someappname::Stripe::SubscriptionService
What am I doing wrong here?
I suspect it's spring, try this
bin/spring stop
And then start rails console, stopping Spring will force Rails to load your app fresh
Also,
if the name of your module is Someappname, then the directory name should be app/someappname and not some_app_name
Hope that helps!
Rails does auto-load everything under /app, but there's a caveat. First level of directories is not becoming a part of module name. That's why you can have class User in /app/models/user.rb (and not class Models::User).
Solution: place your code into some folder under /app. I usually call it /app/lib or /app/custom or something like that.
/app/custom/some_app_name/stripe/subscription/subscription_service.rb
(and yes, make sure that names in your filepath correctly represent names in your module path. You can't have directory some_app_name for module Someappname, but you can for SomeAppName)
This is probably a simple "Navigating directory" problem since I'm new but..
Inside of a file named tasks_lists.rb I am attempting to save the text inside of a text file to a variable named draft.
Here is that code:
class FivePoints::Tasks
attr_accessor :draft, :efile, :serve
def self.start
binding.pry
draft = File.open("./text_files/draft.txt")
efile = File.open("./text_files/efile.txt")
serve = File.open("./text_files/serve.txt")
self.new(draft, efile, serve)
end
def initialize(draft , efile, serve)
#draft = draft
#efile = efile
#serve = serve
end
end
I have tried to pry around, but no luck. I have also tried variations such as ("../text_files/efile.txt"), ("/text_files/efile.txt"). I will attach an image of the directory tree as well.
I am simply trying to eventually puts out some text in the CLI for a sample program. That text will be in the draft.txt file. Any ideas?
Assuming you only want to run this locally, then find out the path of the file (you can view its properties from file explorer), and use that explicitly in your code.
Find the exact path (If you're on UNIX it'll be something like /home/USER/projects/five_points/bla....) then replace your string with the exact full path and you'll be able to open the files as needed
It looks like your files are in a directory called five_points but your code is under lib so one level higher ?
Also, you need to be sure you open the file for appending.
draft = File.open("./five_points/text_files/draft.txt", "a")
I need to delete the directory after its creation but I am getting an error because the directory is still in use by ruby.exe or another ruby process is there any way that I can close the directory like closing the file so that it will delete after its creation. When i reload the page and then try to remove the directory then the directory successfully delete.
Here is the code which i am trying
if Dir.exists?("first/test")
FileUtils.rm_rf("first/test")
FileUtils.mkdir("first/test")
else
Dir.mkdir("first/test")
end
Test folder does contain sub directories and files.
The stream was not closing after writing files in rubyzip class
I have modified the code in rubyzip class like this
disk_file = File.open(diskFilePath, "rb")
io.get_output_stream(zipFilePath) { |f|
f.puts(disk_file.read())
}
disk_file.close
There are two main problems with your code, I think:
According to Ruby documentation, Dir.exists? is deprecated and should not be used. Use Dir.exist? (without the 's') instead;
You are trying to create a directory structure with FileUtils.mkdir or Dir.mkdir when, in fact, you need a 'stronger' method: FileUtils.mkdir_p.
Try this:
if Dir.exist?("first/test")
FileUtils.rm_rf("first/test")
FileUtils.mkdir_p("first/test")
else
FileUtils.mkdir_p("first/test")
end
And see the corresponding documentation.
I believe that doing
FileUtils.mkdir("first")
FileUtils.mkdir("first/test")
would work fine, although I haven't tested it, because the second dir ('test') would be create inside an existing one. But if you need to create a whole structure in a single command you'd need the -p flag using a bash command and the corresponding FileUtils.mkdir_p method.
Let me also point you that this if..else structure is not good this way. You want to create the directory structure in both if and else, and if the same command appears in both if and else, it must be taken out of the if..else, like this.
if Dir.exist?("first/test")
FileUtils.rm_rf("first/test")
end
FileUtils.mkdir_p("first/test")
I hope this helps.
I'm writting a ruby gem and I want to let user use your own yaml file configuration, but I don't know how to test if the user_config.yml exists into rails config path.
I want to use user_config.yml only if this file exists into config path.
Rails.root (or RAILS_ROOT in earlier versions of Rails) gives you the path of the application root. From there, you can see if the file you're interested in exists.
e.g.
path = File.join(Rails.root, "config", "user_config.yml")
if File.exists?(path)
# do something
end
you can access your Rails App's load path with the $LOAD_PATH variable which is of type Array.
Thus
if $LOAD_PATH.include?('/path/where/user/should/put/their/user_config.yml')
# EDIT
config_options = File.new('rails_app/config/user_config.yml', 'r')
else
# EDIT
config_options = File.new('path/to/default/config.yml', 'r')
end
There is also another useful method for the Ruby File class called #exists? which will of course check to see if a file exists. See File class docs for more.
Hope this gets you started. Your question was rather vague, reply with more details if you need more help.
I'm using the carrierwave gem to upload files.
I have built a system for users to flag images as inappropriate and for admins to remove the images. From what I can tell, calling destroy on the image will only remove the path name from the table.
Is there a way to have carrierwave actually remove the file itself? Or should rails automatically remove the file when I destroy the image path?
Like #mu_is_too_short said, you can use File#delete.
Here's a code snippet you could use as a helper with a little tweaking in your rails app.
def remove_file(file)
File.delete(file)
end
or if you just have the filename stored in file
def remove_file(file)
File.delete("./path/to/#{file}")
end
Not sure what CarrierWave offers for this, but you could use FileUtils in the Ruby standard library with an ActiveRecord callback.
For instance,
require 'FileUtils'
before_destroy :remove_hard_image
def remove_hard_image
FileUtils.rm(path_to_image)
end
Sidenote: This code is from memory.
If one wants to delete a file but does not want to specify the full filename you can use the below.
Can also be used to delete many files or all files in a directory with a specific extension...
file = Rails.root.join("tmp", "foo*")
or
file = Rails.root.join("tmp", ".pdf")
files = Dir.glob(file) #will build an array of the full filepath & filename(s)
files.each do |f|
File.delete(f)
end