Ruby on Rails upload file problem odd utf8 conversion error - ruby

I am trying to upload a file and i am getting the following error:
"\xFF" from ASCII-8BIT to UTF-8
I am pretty much following the rails guides in what they are doing. Here is the code I am using.
file = params[:uploaded_file]
File.open(Rails.root.join('public', 'images', file.original_filename), 'w') do |f|
f.write(file.read)
end
I don't get why it doesn't work. What am I doing wrong?
Update -- Here is the application Trace
app/controllers/shows_controller.rb:16:in `write'
app/controllers/shows_controller.rb:16:in `block in create'
app/controllers/shows_controller.rb:15:in `open'
app/controllers/shows_controller.rb:15:in `create'

I believe this is a change in how rails 3 works with ruby 1.9, since 1.9 supports encodings it will attempt to convert all strings to whatever encoding you have set in your app configuration (application.rb), typically this is 'utf-8'.
To avoid the encoding issue open the file in binary mode, so your mode would be 'wb' for binary writeable:
File.open(Rails.root.join('public', 'images', file.original_filename), 'wb') do |f|
f.write(file.read)
end

I had similar issue with uploading binary files and your solution strangely did not work, but this one had, so here is it for anyone else having the same problem
file.tempfile.binmode
put this line before File.open. I think the reason is that the temporary file is opened in nonbinary mode after upload automatically, and this line switches it to binary, so rails does not try any automatic conversion (which is nonsense in case of binary file).

dst_path = Rails.root.join('public', 'images', file.original_filename)
src_path = params[:uploaded_file].path
IO.copy_stream(src_path, dst_path) # http://ruby-doc.org/core-1.9.2/IO.html#method-c-copy_stream

Related

`write': "\xCF" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError) while writing to file from url

I am getting error:
write': "\xCF" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
from line:
open(uri) {|url_file| tempfile.write(url_file.read)}
relevant code is:
require 'tempfile'
require 'open-uri'
require 'uri'
..
uri = URI.parse(#download_link)
tempfile = Tempfile.create(file_name)
open(uri) {|url_file| tempfile.write(url_file.read)}`
..
It runs completely fine if I run it like ruby lib/file.rb, but gives error when I run it in rails environment: rails runner lib/file.rb.
Most questions with this error refer to gem installation scenarios. My guess that I have to include/update some gems, but have no idea which.
Use force_encoding:
open(uri) {|url_file| tempfile.write(url_file.read.force_encoding("UTF-8"))
Accepted answer is fine, but I think it is worth mentioning that You can also set encoding when creating/opening Tempfile, for example:
Tempfile.new("file.pdf", encoding: 'ascii-8bit') # or 'utf-8'
This should solve the problem.
data = URI.parse(#download_link).read
tempfile = Tempfile.create(file_name)
tempfile.binmode # This will help deal encoding problem with download files from the internet
tempfile.write(data)
binmode is binary mode

Cannot open file ruby.png in Gosu using Learn Game Programming with Ruby

I'm using the Learn Game Programming with Ruby book and I'm trying to just execute the sample code.
I get the following error, using the sample code.
❯ ruby WhackARuby/WhackARuby_1/whack_a_ruby.rb code
/Users/noahclark/.rvm/gems/ruby-2.2.1/gems/gosu-0.10.4/lib/gosu/patches.rb:40:in `initialize': Cannot open file ruby.png (RuntimeError)
from /Users/noahclark/.rvm/gems/ruby-2.2.1/gems/gosuu0.10.4/lib/gosu/patches.rb:40:in `initialize'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:15:in `new'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:15:in `initialize'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:70:in `new'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:70:in `<main>'
The sample code looks like this:
require 'gosu'
class WhackARuby < Gosu::Window
def initialize
super(800, 600)
self.caption = 'Whack the Ruby!'
#image = Gosu::Image.new('ruby.png')
end
end
Any thoughts on what could be going on here? I've tried changing the offending line to #image = Gosu::Image.new('./ruby.png') for example and that didn't help.
I doubt this is the cause, but my ruby version is ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
EDIT includes file path:
Invariably the problem is because the file doesn't exist where you think it is.
There are many ways to reference a file. The File documentation has expand_path, realpath, absolute_path, all of which make it easy to reference a file based on an absolute or relative path, and relative to the currently running file, application or a particular directory. How to use them is covered in their examples.
It's important to make sure you know what directory the code considers it's current-working-directory, and/or where the file is. The first is important when using a relative path, and the second is if you don't want to care about your current path and know that the file ALWAYS exists in a certain place.
And, then there's also the case when the file's name is different than what you think it is, or it doesn't even exist.
In my case the solution was using a more explicit filepath:
Gosu::Image.new("#{__dir__}/ruby.png")

Sketchup ruby definitionlist load method error: Invalid component file

When I create a script file and load it from the console with:
load '//192.168.0.0/Mağaza/script.rb'
I get 'Invalid component file' error for:
someModel = Sketchup.active_model.definitions.load '//192.168.0.0/Mağaza/Definitions/model.skp'
But when running the code directly in console, it works.
Any idea why?
DefinitionList.load is a completely different method from Ruby's load.
To load a component from a URL you need to use model.definitions.load_from_url:
http://www.sketchup.com/intl/en/developer/docs/ourdoc/definitionlist#load_from_url
After two days, I figured out that the problem was the encoding of 'ğ' in the folder name (mağaza). I tried ANSI and UTF-8 encoding in my script file but nothing changed. But when print the path name in the console, it turned out that the character was not encoding properly.

Net::FTP and open-uri hanging while trying to put binary file to from URL to FTP server

I have a scenario where I have an S3 URL with binary content that needs to be transferred to an FTP server. I'm using Net::FTP, open-uri, and Ruby 2.0.0-p353.
require 'net/ftp'
require 'open-uri'
Net::FTP.open(x,y,z) do |ftp|
ftp.putbinaryfile(open(an_s3_url), 'remote_filename', 4096)
What the code above does is create a temporary open-uriXXYY-* temp file in the current dir, but then hangs. I have no idea why. Interrupting the function produces the following trace:
home/ubuntu/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/net/ftp.rb:434:in `accept': Interrupt
from /home/ubuntu/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/net/ftp.rb:434:in `transfercmd'
from /home/ubuntu/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/net/ftp.rb:543:in `block (2 levels) in storbinary'
from /home/ubuntu/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/net/ftp.rb:199:in `with_binary'
from /home/ubuntu/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/net/ftp.rb:542:in `block in storbinary'
from /home/ubuntu/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
from /home/ubuntu/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/net/ftp.rb:541:in `storbinary'
from /home/ubuntu/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/net/ftp.rb:690:in `putbinaryfile'
The workaround is to retrieve the file and then put it, but this feels clunky. Is there a reason I can't do it this way, or am I missing something obvious? Same problem with or without blocksize included.
The answer was to use passive FTP -- this was the obvious(?!) thing I was missing.
It is by default set to false.
Net::FTP.open(x,y,z) {|ftp| ftp.passive = true ... }
Worked like a charm.

Rails 3.1 Possible Bug in Asset Pipeline and Uglifier

I ran into a problem deploying on Heroku do to a failure in the rake task
rake assets:precompile
At the bottom is the error I get if I integrate
Rails 3.1
Jquery calendar: https://github.com/themouette/jquery-week-calendar
Twitter bootstrap
The error happens from uglifier.
I suspect that problem could be related to the inclusion of many localizations for the calendar.
I worked around the error by setting:
# Compress JavaScripts and CSS
config.assets.compress = false
I was not able to examine the files as the temporary files are cleaned up. I also could not get the debugger in RubyMine to stop at a breakpoint.
Any ideas if this is a bug? Any way to get the temporary files to not get deleted? Any way to make the RubyMine debugger work on the rake task (yes, tried the obvious, using EAP 112-291.
rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted! Unexpected character '' (line: 21454, col: 0, pos:
641761)
Error
at new JS_Parse_Error (/tmp/execjs20111231-15374-1fve7h4.js:497:22)
at js_error (/tmp/execjs20111231-15374-1fve7h4.js:505:15)
at parse_error (/tmp/execjs20111231-15374-1fve7h4.js:596:17)
at Object.next_token [as input] (/tmp/execjs20111231-15374-1fve7h4.js:839:17)
at next (/tmp/execjs20111231-15374-1fve7h4.js:943:37)
at Object.semicolon [as 1] (/tmp/execjs20111231-15374-1fve7h4.js:986:38)
at prog1 (/tmp/execjs20111231-15374-1fve7h4.js:1527:28)
at simple_statement (/tmp/execjs20111231-15374-1fve7h4.js:1123:35)
at /tmp/execjs20111231-15374-1fve7h4.js:1031:35
at /tmp/execjs20111231-15374-1fve7h4.js:1510:32
You will probably find that one of you js files has a syntax error somewhere. This could be a missing semicolon at the end of a block, or some other minor problem. Often browsers will still load the js and it will work, but uglifier cannot compress it with those errors. I would start looking in the localisation files first.
One way to find out which file contains the error is to re precompile locally with a minimal set of files and add things one by one until it breaks. If it is due to a missing semicolon, the breakage will the second-last file you added.
Mine precompiled after I removed a stray "debugger" statement. Woops.
If anyone reading this thread encounters issues with unicode characters or "invalid byte sequence in UTF-8" in your rails app, try putting this in your production.rb file:
# override default uglifier options so we don't mangle unicode
config.assets.js_compressor = Uglifier.new(output: {ascii_only: true})
In my case, the uglifier was converting strings in my javascript like \udbff into UTF-8 characters í¯¿ which ultimately was breaking some unicode regex. (This was happening with turbo-sprockets and codemirror but you might encounter it anytime your javascript relies on ASCII representations of unicode characters.)
The I18N file "jquery-ui-i18n.js" has a bad character before each comment.
Looking at the first two lines with "more" in a shell, shows the wrong character:
<U+FEFF>/* Afrikaans initialisation for the jQuery UI date picker plugin. */
/* Written by Renier Pretorius. */
After having removed this character it works.

Resources