Why can't Rails find this custom .yml file? - ruby-on-rails

In applications.rb I added this:
SOME_CONFIG = = YAML.load_file(File.expand_path('../some_config.yml', __FILE__))
I placed the file in:
/some/folder/myapp/config/some_config.yml
Then when I run 'rails server' I get an error:
.. in 'initialize' : no such file or directory /some/folder/myapp/config/some_config.yml (Errno::ENDENT) .... 'open' .... 'load_file' ....
Is this a permissions issue?

I don't believe this to be a permissions issue.
1) Double check your spelling to make sure there are no typos.
2) Try reading another file, and see if it works.
3) Try: YAML.load_file(File.join(Rails.root, 'config', 'some_config.yml')) (Your error message shows that the issue is not in expanding the filename, but you never know).
4) Can you access it from the console?

my_data = HashWithIndifferentAccess.new( YAML.load(File.read(File.expand_path('../../some_file.yml', __FILE__))) )
now due to HashWithIndifferentAccess, you can get data in any of the given below syntax
my_data[:some_key] or my_data['some_key']

Related

Setting a default editor in Pry

I'm asking about setting a default editor for Pry to use. I'm working on a Rails
app. I created a file named ".pryrc" immediately inside my working directory.
In this file, I wrote this line of code (based on what I read on Github :
Pry.config.editor = proc { |file, line| "sublime +#{line} #{file}" }
This doesn't seem to work. when I try the command ".sublime company.rb", I 'd get
this error:
Error: there was a problem executing system command: sublime company.rb
Can someone tell me what I'm doing wrong please?
Change the configuration to:
Pry.config.editor = proc { |file, line| "sublime #{file}:#{line}" }
You start the editor in Pry by using the edit command.
For example to open test.rb at line 30 use:
edit test.rb:30
See here for more details
For those who have the same problem as mine.Perhaps, you have trouble launching the editor even outside Pry. First thing, make sure to check if the sublime command exists in ur PATH. if not, you probably need to create a symlink between the command and the corresponding path to your app within /usr/local/bin. For more information, see here.

Ruby double pipe usage

I have a file which have different paths for remote server and local server.
Remote server path:
"/app/public/front-end/public/JSONModels/IdPairing/Text.json"
Local server path:
"public/front-end/public/JSONModels/IdPairing/Text.json"
I basically wanna make sure my app finds correct path for the file regardless of which server I'm at.
So I found something like double pipe ( || ) in Ruby syntax like below:
File.read("/app/public/front-end/public/JSONModels/IdPairing/Text.json" || "public/front-end/public/JSONModels/IdPairing/Text.json")
But it seems like it only reads the first path. How do you make sure it reads the second path if the file is not being found in the first path?
Thanks
A lazy way is:
begin
File.read("/app/public/front-end/public/JSONModels/IdPairing/Text.json")
rescue
File.read("public/front-end/public/JSONModels/IdPairing/Text.json")
end
Why it doesn't work for you:
irb(main):001:0> File.read("some made up name")
Errno::ENOENT: No such file or directory - some made up name
from (irb):1:in `read'
from (irb):1
from /usr/bin/irb:12:in `<main>'
irb(main):002:0> File.read("some made up name" || "Gemfile")
Errno::ENOENT: No such file or directory - some made up name
from (irb):2:in `read'
from (irb):2
from /usr/bin/irb:12:in `<main>'
As you can see, when you try to read a file that doesn't exist, Ruby explodes.
That's what exist? is for.
What you should do
irb(main):003:0> filename = File.exist?("some made up name") ? "some made up name" : "Gemfile"
=> "Gemfile"
irb(main):004:0> File.read(filename)
That's one way to do it, at least.
I would suggest to put those paths into an environment specific config file. Take a look at railsconfig/config on GitHub.
After you have installed the gem, you can add a configuration to config/settings/<RAILS_ENV>.yml like
json_file: /app/public/front-end/public/JSONModels/IdPairing/Text.json
where <RAILS_ENV> can be development, test, production or a custom environment.
And in your code, just
File.read(Settings.json_file)
If you want to try these in sequence, define a list first:
FILES = [
"/app/public/front-end/public/JSONModels/IdPairing/Text.json",
"public/front-end/public/JSONModels/IdPairing/Text.json"
]
Then you can simply open the first one that exists:
path = FILES.find { |f| File.exists?(f) }
File.open(path)
The problem was that "string" || "string" will never work, the first string is logically true and the rest is irrelevant and ignored.

Writing a file to a specific path in ruby taking the file name from excel

I am having some trouble writing a file to a specific path taking the file name from excel. Here is the code which I am using
out_file = File.new (#temp_path/ "#{obj_info[3].to_s}","w")
"#{obj_info[3].to_s}" = sample.txt
The value sample.txt comes from Excel during run time
#temp_path = "C:/Users/Somefolder/"
The error displayed is:
NoMethodError: undefined method `[]' for nil:NilClass
However, if the code is:
out_file = File.new ("#{obj_info[3].to_s}","w")
it successfully creates a file called sample.txt in the default directory. However, I want it to be stored in a specific directory and the file name needs to be passed from Excel.
Any help is much appreciated.
I believe your problem is because there a space between / and "
#temp_path/ "#{obj_info[3].to_s}
and I guess you want to build a path.
My advice is that you use File.join
f_path = File.join(#temp_path,obj_info[3].to_s)
out_file = File.new (f_path,"w")
Let me know if that solved the problem
You have 2 problems:
obj_info is nil so you make an error reading the value from excel, the error you get indicates it is on an array, in the code you published the only thing that's an array is what you read from excel.
Print the contents with p obj_info right before your code to check.
#temp_path and {obj_info[3].to_s} need to be concatenated to make a path.
You can do that with File.join like Mauricio suggests or like this
out_file = File.new ("#{#temp_path}/#{obj_info[3]}","w")
You can drop the to_s in this case.
It would be better if you publish the whole of your script that is meaningful.

Error when uploading file using Rails gem Net::SFTP: no such file

I am trying to upload a file to a server using the Net::SFTP gem. My code is as follows:
remote_path = "path/of/remote/file.txt"
local_path = "path/to/local/file.txt"
Net::SFTP.start("SERVER", "USER", :password => "PASSWORD") do |sftp|
sftp.upload!(local_path, remote_path)
end
When I execute this I get:
Net::SFTP::StatusException (Net::SFTP::StatusException open path/of/remote/file.txt (2, "no such file"))
I think the issue is that the entire remote_path of the directories doesn't exist yet. If I give it a path of directories that do exist, it will create the directories. I want the upload to also create the directories in the path if they don't exist yet. Is this indeed the issue, and if so, how can I create the directories with SFTP?
you will have to create the directory first
sftp.mkdir! "/path/to/directory"
Here's a little snippet to check if the dir exists and create it if not:
if !sftp.dir.entries("root").map { |entry| entry.name }.include?("new_dir")
sftp.mkdir("root/new_dir")
end

Loading yaml from file and join fails

I try to load a yaml file into an array but it fails with undefined method `join' for "a b c":String
# Check certain temporarily emails
# Throw notice not accepted use other email
require 'yaml'
bad_hostnames = YAML::load(File.read("#{Rails.root}/config/bad_hosts.yml"))
if /^(#{bad_hostnames.join("|")})$/.match(host)
errors.add(:email, "Please not use a disposable mailbox")
end
So i required yaml before and on top of the model, controller where I load the yml in:
require 'yaml'
Still same result, in rails console this works flawlessly, what am I missing?
The above code is inside my user.rb model, in console it works
EDIT:
bad_hosts.yml looks like (shortened) 1 provider the line
0-mail.com
10minutemail.com
30minutemail.com
4warding.net
Your .yml file is not a YAML file.
This would make it .yml file.
- 0-mail.com
- 10minutemail.com
- 30minutemail.com
- 4warding.net
But when you want to load just a file line by line try the following:
lines = IO.readlines("#{Rails.root}/config/bad_hosts.yml")
# note: lines end in "\n"

Resources