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!
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
config/database.yml
default: &default
adapter: sqlite3
encoding: unicode
development:
<<: *default
database: development.sqlite3
username: admin
password: wsxqaz
server: localhost
# port:
ruby code:
require "yaml"
require "active_record"
# shorten lines with this namespace
include ActiveRecord::Tasks
# trying to use db folder
DatabaseTasks.db_dir = './db'
db_config_file = "./config/database.yml"
db_config = YAML.load_file(db_config_file)
db_type = db_config['development']
#sldbtask = SQLiteDatabaseTasks.new(db_type, root = './')
#sldbtask.create
I've tried changing the root argument but that doesn't make any difference either.
What do I need to get my db file into the db directory? It creates it okay and the table stuff is pending. First things first.
I believe that the database is supposed to be generated in the root directory. The db folder is used for activerecord's schema and migration files. Why is it so important that the database generates in the db directory?
EDIT
I just found an easier way to do this. After you require all of the files you want, then enter set :database, {database:"./db/myDatabase.db"}. If you already have the line set :database in your code, just add that to it. Good luck!
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
I used rvm upgrading to 1.9.2-p180. during the installation, libyaml 0.1.3 was fetched and installed.
Now when I have my database.yml in a rails project like this:
defaults: &defaults
adapter: mysql
encoding: UTF-8
username: root
password:
host: localhost
engine: InnoDB
test:
<<: *defaults
database: my_app_test
in console,
YAML.load_file("config/database.yml")
The result is:
{"test" => {"apapter" => "mysql"...}}
The problem is the "database" key is missing. it seems not to merge the defaults node and test node togther, rather it just copy the default node.
any idea?
Anyway, upgrade to ruby-p290 solved the issue.
If I understand you correctly, I can't duplicate that, also using 1.9.2.180:
irb(main):002:0> YAML::load_file('./test.yml')
=> {"defaults"=>{"adapter"=>"mysql", "encoding"=>"UTF-8", "username"=>"root", "password"=>nil, "host"=>"localhost", "engine"=>"InnoDB"}, "test"=>{"adapter"=>"mysql", "encoding"=>"UTF-8", "username"=>"root", "password"=>nil, "host"=>"localhost", "engine"=>"InnoDB", "database"=>"my_app_test"}}
(Scroll right to see: "database"=>"my_app_test"}} )
Not sure if this is an answer as such, but I couldn't put the code in a comment. If I have the wrong end of the stick I'll edit or delete...
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