Rails - how to load the whole directory of files in /lib? - ruby-on-rails

I have in the lib directory following structure:
/lib/dir_a/dir_b/dir_c/
In dir_c are stored images.
I am trying to load these images and display them in views. I have tried to set up the path to the images in views, but I got the 404 error.
So I did following: created a file in the initializers folder and into this file I put:
Dir[Rails.root + 'lib/dir_a'].each do |file|
require file
end
For loading all content stored in the dir_a directory (which involves as subdirectories as files).
But when I restarted server, I got this error:
...dependencies.rb:251:in `require': cannot load such file -- /Users/radek/rubydev/EDI/lib/brands (LoadError)
I also tried stuff like
Dir[Rails.root + 'lib/dir_a/'].each do |file|
or
Dir[Rails.root + 'lib/dir_a/**'].each do |file|
But none of those helped me.
Thus, is there any way to load a content from /lib directory and work with them in views?

Dir[Rails.root + 'lib/**/*.rb'].each do |file|
require file
end

Related

Rails same file name for different classes

Having model Price (models/price.rb)
class Price
end
Also in lib directory I have import/detector/price.rb file
class Import::Detector::Price
end
Lib directory added to autoload paths via
config.autoload_paths += Dir["#{config.root}/lib/**/"]
So running Price.new I get error
Unable to autoload constant Price, expected lib/import/detector/price.rb to define it.
Import::Detector::Price.new is ok
What was my mistake?
UPD.
The most interesting
Also having this file models/car/property/price.rb
class Car::Property::Price
end
And everything ok with it. Car::Property::Price is available.
Try
config.autoload_paths += Dir["#{config.root}/lib"]
instead of
config.autoload_paths += Dir["#{config.root}/lib/**/"]
Using **, Dir will return every directory under lib and will put them all (as root directories) in the load path. It should only be the main lib directory in the load path as rails will work out the subdirectories using the namespace.
Rails autoloading — how it works, and when it doesn't

Getting "Errno::ENOENT: No such file or directory # rb_sysopen" while reading the CSV file from the AWS S3

I have application which is deployed to Heroku. I have added functionality for uploading users thorough the CSV. For this I have provided CSV upload functionality (Used Paperclip gem).
Here is my code for reading the file and creating new user
def import(file)
CSV.foreach(file.path, headers: true) do |row|
row_hash = row.to_hash.values
data = row_hash[0].split("\t")
.
.
.
end
On the local it is working fine. But on the heroku it is giving me following error
Errno::ENOENT: No such file or directory # rb_sysopen - https://s3.amazonaws.com/..../..../sample_csv(2).csv
I referred following links
Errno::ENOENT (No such file or directory) in amazon-s3
File reading from Amazon server, ruby on rails, no match route
but didn't any success. For more debugging, I tried same url from my local rails console and it is giving me the same error.
2.2.2 :008 > cp = "https://s3.amazonaws.com/..../..../sample_csv(2).csv"
2.2.2 :008 > f = File.open(cp, "r")
Errno::ENOENT: No such file or directory # rb_sysopen - https://s3.amazonaws.com
Also tried open uri http://ruby-doc.org/stdlib-2.1.0/libdoc/open-uri/rdoc/OpenURI.html.
I can download the same file from the browser.
Can any one let me know how to resolve this error. Is there any bucket permission issue (I have already provided open access for the bucket).
Try this
require 'open-uri'
require 'csv'
def import(file)
CSV.new(open(file), :headers => :true).each do |row| #First open the file using open
row_hash = row.to_hash.values
data = row_hash[0].split("\t")
.
.
.
end
For more info you can refer this link

Rails 4, Paperclip, Upload in FTP. How to rename?

I'm using Rails 4 and Paperclip.
Beacuse I need to upload files on FTP server i'm using this great gem:
https://github.com/xing/paperclip-storage-ftp
Everything works perfect in local, but in FTP I can't rename files using this code:
def rename_myfile
if self.rename.present?
path = self.myfile.path
FileUtils.move(myfile.path, File.join(File.dirname(myfile.path), self.rename))
self.myfile_file_name = self.rename
end
end
I got an error:
No such file or directory # sys_fail2 - (/myfiles/19/original/myfileOriginalName.jpg, /myfiles/19/original/myfileRenamedName.jpg)
How to enter in ftp with FileUtils.move???
Create and Delete are working very well!
https://github.com/xing/paperclip-storage-ftp/issues/28
You have to build the full path to the file not just the file's dirname and name. change your FileUtils.move line to this:
orig_full_path = Rails.root.join "public", myfile.path # assuming they're saved in your public directory
new_full_path = Rails.root.join "public", File.dirname(myfile.path), self.rename
FileUtils.move orig_full_path, new_full_path
The idea here is to get the absolute path to your files. Before you were just giving FileUtils this path: /myfiles/19/original/myfileOriginalName.jpg which means it will look for the file in a folder /myfiles in the root of your file system. But they're actually in your Rails folder. So you should use Rails.root.join to get the true absolute path: /Users/me/my_rails_project/public/myfiles/19/original/myfileOriginalName.jpg.

" require './example_user' " results in " LoadError: no such file to load -- ./example_user "

I'm following Hartl's tutorial.
The instructions say to "create the file example_user.rb in your application root directory". I'm assuming that's just under rails_projects/sample_app. I then start the console and type
require './example_user'
but get the error message
`LoadError: no such file to load -- ./example_user`.
Another answer said that you have to use the mv function to move the file to the actual root directory, but I was unable to make sense of that. Is the root directory not simply in sample_app?
Thanks for any help!

GridFS + CarrierWave + nginx unable to get file

In my project I upload audiofiles to GridFS using CarrierWave gem. After uploading file is saved to GridFS properly but in my application I am unable to get it from GridFS with MongoFiles Tool or with GridFS-nginx module.
mongofiles get audiotracks/4dfb70d6bcd73f3488000002/data
command leads to this error:
assertion: 13325 couldn't open file: audiotracks/4dfb70d6bcd73f3488000002/data
The only way to get file is to use rails console and it works fine:
cc = Mongo::GridFileSystem.new(Mongo::Connection.new.db("test")).open('audiotracks/4dfb70d6bcd73f3488000002/data', 'r')
cc.read
So if you have encountered problem like this or have some ideas - plz let me know.
mongofiles get will try to write the file to disk with the same name and path as in GridFS.
Assertion 13325 happens when GridFS can't write the file like this.
You should check if the file path exists and you have the permission to write the file. Alternatively you could just provide a file name with the --local parameter.
mongofiles --local mytrack.mp3 get audiotracks/4dfb70d6bcd73f3488000002/data

Resources