I am trying load a file in controller, like
Avatar.all.each do |avatar|
if avatar.avatar_file_name
file = "lib/data/#{avatar.avatar_file_name}"
image = MiniMagick::Image.open("#{file}")
...
end
end
But whenever I run this code, I get the error message
MiniMagick::Invalid
I already tried to reinstall the imagemagick as is mentioned here, but it didn't really help me.
Where could be a problem? Am I missing a component or something?
Thank you
Maybe you should check if file exists first
if File.exist?(file)
image = MiniMagick::Image.open(file)
end
To get path to the file you should do something like this:
file = "#{Rails.root}/lib/data/#{avatar.avatar_file_name}"
btw lib is not the best place to save your images.
Related
I'm trying to work with zip file. I have a document I want to extract, add content to it, replace it and remove the one I extracted.
Whenever I just extract the file, add content, and then delete it - every thing works. But when I try to use replace -
#zipfile.replace(document_entry, document_path)
or add and then delete, I get different errors.
If I just add the file without deleting it - it works ok.
If I add the file and delete it or use replace function, it doesn't work and raises an error.
for example, when I use this code, I get:
No such file or directory # rb_sysopen - /home/shiran/Desktop/pop/app/documentfirst.xml (Errno::ENOENT)
Any help would be appreciated, I'm stuck and have no idea what else to look for.
document_entry = "word/document.xml" #this is the path in my zip I opened before
document_path = "/home/shiran/Desktop/pop/app/documentfirst.xml"
zipfile.extract(document_entry, document_path)
fd = File.open(document_path)
file = Nokogiri::XML(fd)
#this is where I add content to my file which works ok
wbody = file.xpath("//w:r")
wtstart = Nokogiri::XML::Node.new "t", file
wtstart.content = "I am adding a new content after the header!"
wbody.children.before(wtstart)
File.write(document_path, file.to_xml)
zipfile.remove(document_entry)
zipfile.add(document_entry, document_path)
fd.close
file_exist = File.exist?(document_path)
if file_exist
#raises an error here -
File.delete(document_path)
puts "file exists" #(it does puts file exists)
end
I have a function in my activejob that extracts a specific file from a zip file. Following code extracted empty file.
def extract_file(from, name)
to = get_local_dest(name)
Zip::File.open(from) do |zip_file|
entry = zip_file.glob(name).first
puts entry.get_input_stream.read
entry.extract(to)
end
return to
end
I added a debugger and ran following line of code in console then the extracted file was not empty.
entry.extract(to)
Can anyone help me with this issue? Why this function is extract empty file when it runs in activejob?
your code seems to be fine. Please make sure your file is not saving again in code that proceeds.
There are chances that it extracted fine but later in code it was save again in empty form. This had happened to me in past :)
Let me know if this resolved your issue. :)
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.
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.
I have directory that contain multiple file
i need to load on specified file.
i know i can use loadfile(path) but how i need to specify which file to load
thank you
Jp
I'm not 100% sure I understand what you are asking, but here's my take on it. If you know the directory you need to load the file from, you'd just prefix it to the name of the file:
local f, error = loadfile(mydir .. "/my_file")
Note that this reads and parses the file. To actually execute it, you need to invoke the function you get back from loadfile() (so f() in this example). If there is an error, loadfile() returns nil and an error message.
path = "./path/to/a/file.lua"
local myreturn1, myreturn2 --[[etc]] = assert(loadfile(path))(myarg1, myarg2, myarg3 --[[etc]])