irb `initialize': No such file or directory # rb_sysopen - - ruby-on-rails

I try to modify paperclip image from my model after a server change.
I have a file with id of my model and name of image like that
3;3da442247510f15c07fead8f41cd1c9441694b02.jpg
4;e3b652bfe16759002d6dd3da608475069df1c02e.jpg
5;0699ab4040c4d4f6e8ec390170014661bc5f6d96.jpg
I have write a script to find the file and give ti to my model
File.open("titi.txt", "r") do |f|
f.each_line do |line|
tab=line.split(";")
puts "id => #{tab[0]} | nom => #{tab[1]}"
c=Contact.find(tab[0])
file = File.open("#{Rails.root}/public/system/contact/#{tab[1]}")
c.photo = file
file.close
c.save!
end
end
I launch my script like that
RAILS_ENV=production rails runner script.rb
But I have this error
script.rb:6:in `initialize': No such file or directory # rb_sysopen - /var/www/contactmedia/public/system/contact/3da442247510f15c07fead8f41cd1c9441694b02.jpg (Errno::ENOENT)
but when I do
ls -la /var/www/contactmedia/public/system/contact/3da442247510f15c07fead8f41cd1c9441694b02.jpg
I have a file....
What is wrong in my script ? Whant can I test to move my images.
Thanks
EDIT
it's OK i have found why I read a windows file so i have a \r at end of line. I read my file like that now
file = File.open("#{Rails.root}/public/system/contact/#{tab[1].chom‌​p}")

Related

No such file or directory # rb_sysopen -

I want to resolve this error.
No such file or directory # rb_sysopen - zipUpTest/1051687701.jpg
The following is the source code.
Zip::File.open_buffer(obj) do |zip|
zip.each do |entry|
ext = File.extname(entry.name)
file_name = File.basename(entry.name)
next if ext.blank? || file_name.count(".") > 1
dir = File.join(dir_name, File.dirname(entry.name))
FileUtils.mkpath(dir.to_s)
zip.extract(entry, entry.name) {true}
file_name.force_encoding("UTF-8")
file_new_name = "#{dir_name}/#{file_name}"
File.rename(entry.name, file_new_name)
#input_dir << file_new_name
end
end
end
Zip::OutputStream.open(zip_file.path) do |zip_data|
#input_dir.each do |file|
zip_data.put_next_entry(file)
zip_data.write(File.read(file.to_s))
end
end
That means there is no file, how to deal with this problem?
You have to ensure the file zipUpTest/1051687701.jpg exists relative to where the process was run, not where the program is saved.
For example, let's say your program is /home/taizo/program. If you're in /home/taizo and run ruby program then it will look for /home/taizo/zipUpTest/1051687701.jpg. If you're in /tmp and run ruby /home/taizo/program the program will look for /tmp/zipUpTest/1051687701.jpg.

delete/remove temp zip file in ruby on rails

i am creating temp zip file in rails tmp directory as follows:-
zipfile_name = Rails.root.join("tmp/abc.zip")
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
Dir[File.join(data, '**',)].each do |file|
zipfile.add(file.sub(dir+"/", ''), file)
end
end
zip_data = File.read(zipfile_name) # sending zip file.
zipfile_name.try(:unlink)` # try to remove or unlink the zip but not working.
temp zipfile_name was abc.zip20140816-8931-1yl3g60 and i want to delete it after sending but it's not found.
File.delete(zipfile_name)
Try file delete command it should work out.

Rake db:seed can't find .xml file

Total ruby on rails newbie here.
I'm trying to populate a database within a ruby on rails framework. This is what my seeds.rb file looks like:
f = File.open("db/courses.xml")
doc = Nokigiri::XML(f)
f.close
doc.css("course").each do |node|
children = node.children
Course.create(:name => children.css("name"),
:description => children.css("description"))
end
I haven't been able to test this code, because the rake db:seed command keeps saying "No such file or directoy --- courses.xml". Note that I've both tried "courses.xml" and "db/courses.xml" as paths.
The file courses.xml is in the same folder as the seeds.rb file.
Try specifying the path from the root of the application. Like this:
f = File.open(File.join(Rails.root, 'db', 'courses.xml'))

Why do I get "Errno::ENOENT: No such file or directory" when joining a filename?

I have a file I'm trying to open in a Rails application. For some reason Ruby is splitting the name of the file.
For example:
root = Rails.root
path = root.join('lib/tasks/filename.shp')
puts path
What is output is /lib/tasks/filename/shp.
Then I run the command:
factory = Region::GEOFACTORY
RGeo::Shapefile::Reader.open(path, :factory => factory) do |file|
I get the error message:
Errno::ENOENT: No such file or directory - /lib/tasks/filename/.shp
It looks like the file has been split into filename and .shp?
Try
path = File.join(Rails.root, 'lib/tasks/filename.shp')
factory = Region::GEOFACTORY
RGeo::Shapefile::Reader.open(path, :factory => factory)

How to use File.open inside a Rails Rake task?

I need to create a recurring task that creates (or edits) product records from an external text file. From inside irb:
>> f = File.open(<filename>) # file in same directory path
No issues.
But when pasted into a Rake task file, the script always bombs "File not found". (Rails 3.1, Ubuntu.)
namespace :sap do
desc "uploads data from raw SAP file"
task :upload => :environment do
f = File.open("sap_pnlist_20111010a.csv")
records = f.readlines
records.each {|row|
... etc etc ...
}
end
end
Suggestions?
If the file is somewhere inside your Rails root, use
Rails.root.join('grandparent_dir', 'parent_dir', 'file.txt')
If the file is not in your Rails root, you must give it the full path.

Resources