Invalid argument # rb_sysopen in Ruby on Rails application - ruby-on-rails

I know this is the common question in this forum, but I tried last nearly 4 hours one by one not working for me. Worked before another method & another resource but not in this method.
Error below:
Invalid argument # rb_sysopen - https://example.s3.amazonaws.com/uploads/Data.pdf
When I use like below: Static File
#file = File.open("#{Rails.root}/public/Data.pdf")
Then
File.new(#file)
That is working but when like below:
Dynamic File
#file = File.open("#{papers.paper.url}") #=> output url: https://example.s3.amazonaws.com/uploads/Data.pdf
Then
File.new(#file)
That is showing error. I have tried string like w w+ r r+ etc.
What I'm doing wrong with this code?
Thanks

Related

Unable to use Mongoid::GridFs put to upload files

I'm just trying to use the mongoid-grid_fs gem in order to upload images and files to MongoDB.
The controller code in question currently looks like this:
grid_fs = Mongoid::GridFs
encFile = File.open(attFile)
File.open(encFile, 'wb') do |f|
f.write(encData)
end
grid_file = grid_fs.put(encFile.path)
I'm getting this error:
undefined method `destroy' for nil:NilClass
/GEMS/bundler/gems/mongoid-grid_fs-2f0e12e85a24/lib/mongoid/grid_fs.rb:150:in `put'
(eval):2:in `put'
/x/app/controllers/runtime/attachments_api_controller.rb:126:in `upload'
FYI line 126 in attachments_api_controller is the grid_fs.put line.
I'm not sure what the issue is.
I have also declared require 'mongoid/grid_fs'
I have put bindings around the issue, I can confirm that encFile.path is correct and it contains valid data.
EDIT 1: I've tried different combinations of trying to put the file like
grid_fs.put(encFile) - grid_fs.put(File.open(encFile.path)) - grid_fs.put(File.read(encFile.path)) but all of these still return the same error.

Error invoking PDFTK when modify PDF in Rails 4

I am developing Rails 4 application where have to modify existing PDF file.
User can write some comments and click then comments write in existing PDF as well. For this, i used gem 'pdf-toolkit'
But i got below error:
Error invoking PDFTK
My Code:
my_pdf = PDF::Toolkit.open("Credit_One.pdf")
my_pdf.updated_at = Time.now # ModDate
my_pdf["SomeAttribute"] = "Some value"
my_pdf.save!
Where is wrong any one have a idea.
Thanks

Ruby on Rails copy S3 file with special characters (%C5) in path

I have a problem with my attachment system on web page. I store them on amazon S3 using paperclip. I have an option to copy attachment to new file. Everything works fine until there are polish special characters in title, like: ŁĄKA.jpg. Then I get an error:
Saving error: Appendix Paperclip::Errors::NotIdentifiedByImageMagickError
/Users/michal/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/validations.rb:79:in `raise_record_invalid'
/Users/michal/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/validations.rb:43:in `save!'
My code:
instance.appendixes.select {|a| a.temporary? && !a.appendix.exists?}.each do |a|
a.appendix = S3File.new(a.s3path)
a.process = false
a.appendix_url = nil
puts "CREATING NEW FILE from (temporary?) appendix: #{a.id}, path: #{a.s3path}, is_public: #{a.is_public}, determine_is_public: #{a.determine_is_public}"
a.is_public = a.determine_is_public
logger.debug("CREATING NEW FILE from (temporary?) appendix: #{a.id}, path: #{a.s3path}, is_public: #{a.is_public}, determine_is_public: #{a.determine_is_public}")
a.save! # bo delayed_job
end
I'm getting error on a.save! when path is like: appendixes/appendixes/242/original/%25C5%2581A%25CC%25A8KA.jpg, but works like charm when it is: appendixes/appendixes/243/original/laka.jpg or another file name without polish letters. Anybody had this kind of problem or have suggestions how to fix it?
Ok, I found what was wrong. I had to replace in a.s3path, the last part with original name (łąka.jpg) and everything works fine. So when I have:
S3File.new(appendixes/appendixes/243/original/łąka.jpg) it works good and finds the correct file on s3 server.

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.

Spreadsheet.open in Rails gives this error "unknown encoding name - MACINTOSH"

I am using Spreadsheet to parse xls files. It's working great. but today one of my user has uploaded a file and it's getting an error "unknown encoding name - MACINTOSH"
My code is:
book = Spreadsheet.open file_path
sheet1 = book.worksheet 0
This is the excel file which is getting the error:
https://www.dropbox.com/s/jv37pk5rpiy9259/testlisttextnonames2.xls
Can you guys please help me to solve this issue.
Running into the same problem. Here is the best i can deduce:
"Macintosh" is not a known encoding in Ruby 1.9+. Try opening a console and running "Encoding.find('Macintosh')". You'll get the same error.
So what is available? In the console: "Encoding.list". One of the options is MacRoman. I'm guessing this is a close second.
So if we change the mapping in lib/spreadsheet/excel/internals.rb for 10000 and 32768 to map to "MACROMAN", it should work.
Tested locally and it does.
I opened a pull request: https://github.com/zdavatz/spreadsheet/pull/51
Some reference links:
* https://en.wikipedia.org/wiki/Mac_OS_Roman
* http://bugs.python.org/issue843590

Resources