I tried to run my rails project on jruby 1.6.7 in 1.9 mode.
I've got troubles with encodings in slim templates and mongodb (with mongoid)
template:
incompatible character encodings: UTF-8 and US-ASCII
and in mongo I see something like Ð\u0090лекÑ\u0081андÑ\u0080
I've tried to solve thus problem by adding in Application.rb
config.encoding = "utf-8"
also I've put
$KCODE = 'u'
Encoding.default_external = Encoding::UTF8
Encoding.default_internal = Encoding::ASCII_8BIT
I've trying to put
#encoding: utf-8
in templates.
Even trying to add in JRUBY_OPTS
-J-Dfile.encoding=UTF8
this all does not helped.
I use jRuby 1.7.0.preview1 on Heroku and I had problem that all my responses ware us-ascii encoded. So I added following configuration to "JRUBY_OPTS". And it helps - my code returns UTF-8 responses.
JRUBY_OPTS: --1.9 -J-Xmx400m -J-Dfile.encoding=utf8
Related
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
I am working on a project with Sitespec, which is a static site generator using RSpec and uses any Rack app. I chose Padrino as a Rack app for Sitespec.
Then, I get the following errors when I do rspec at some URLs of Content-type: image/png type.
Failures:
1) Sitespec GET public/images/simple-image1.png Generate a static image public/images/simple-image1.png
Failure/Error: pathname.write(response.body)
Encoding::UndefinedConversionError:
"\x89" from ASCII-8BIT to UTF-8
Pathname.write(response.body) is going to try to convert it to UTF-8.
But when I use Sinatra instead of Padrino, the error does not occur.
I made simple sample projects for comparison:
Using Padrino https://github.com/Nyoho/sitespec-padrino-sample2
Using Sinatra https://github.com/Nyoho/simple-sitespec-sinatra-test
Question: Why does the Padrino project fail and how do I fix the errors?
By the way, doing monkey patch
module Sitespec
class Artifact
def generate_file
pathname.binwrite(response.body)
end
end
end
eliminates the error. (Replacing Pathname#write to Pathname#binwrite.)
My environment: Rails 4.2.4 Ruby 2.0 & SQL Server 2014 with a collation of SQL_Latin1_General_CP1_CI_AS at the database level.
I got an error
incompatible character encodings: UTF-8 and ASCII-8BIT
when the view found in the database some special characters like ç, á, é, etc.
I've had tried some configurations that I read but nothing worked.
I tried this in the view:
# encoding: utf-8
In the enviroment.erb:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
In the application.rb:
config.encoding = "utf-8"
I tried in view:
<td><%=h role.description.force_encoding("ISO-8859-1").encode("UTF-8") %></td>
It worked correctly but this way is very difficult. I want something that run in all project, like on models for example
I tried on Role class:
self.column.force_encoding("ISO-8859-1").encode("UTF-8")
but not recognize the command
Could someone please help me?
Tanks!
Ale
I solved my problem, finally.
A colleague gave me a tip installing the
tiny_tds
gem and ran properly without any of the settings listed above, just set up the database.yml
development:
adapter: sqlserver
mode: dblib
database: MyDataBase
dataserver: MyServer
Previously I was using ruby-odbc gem
thanks!
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.
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