Encoding::UndefinedConversionError: \xE4 from ASCII-8BIT to UTF-8
You can go with force_encoding
:
require 'net/http'
url = "http://data.linz.gv.at/katalog/population/abstammung/2012/auslg_2012.csv"
File.open('output', "w:UTF-8") do |f|
content = Net::HTTP.get_response(URI.parse(url)).body
f.write(content.force_encoding("UTF-8"))
end
But this will make you lose some acentuation in your .cvs file
If you are deadly sure that you always will use this URL as input, and the file will always keep this encoding, you can do
# encoding: utf-8
require 'net/http'
url = "http://data.linz.gv.at/katalog/population/abstammung/2012/auslg_2012.csv"
File.open('output', "w:UTF-8") do |f|
content = Net::HTTP.get_response(URI.parse(url)).body
f.write(content.encode("UTF-8", "ISO-8859-15"))
end
But this will only work to this file.
Encoding::UndefinedConversionError (\xE2 from ASCII-8BIT to UTF-8): with Rails 4.1 & mysql BLOB datatype
FYI .. it is resolved i changed Data type from BLOB to TEXT and it worked.
BLOB values are treated as binary strings (byte strings). They have no
character set, and sorting and comparison are based on the numeric
values of the bytes in column values. TEXT values are treated as
nonbinary strings (character strings). They have a character set, and
values are sorted and compared based on the collation of the character
set.
The BLOB and TEXT Types
Encoding::UndefinedConversionError: \xC2 from ASCII-8BIT to UTF-8
Your string is in some other encoding, most likely iso-8859-1, so you should run this to convert it:
"\xC2".encode("iso-8859-1").force_encoding("utf-8")
=> "Ã"
See this question for more information related to this issue.
Carrierwave: Encoding::UndefinedConversionError (\xFF from ASCII-8BIT to UTF-8)
My guess would be that you need to use :string
instead of :json
in the ActiveRecord::Migration
.
class AddImagesToGallery < ActiveRecord::Migration
def change
add_column :galleries, :images, :string, array: true, default: [] # add images column as array
end
end
You can check following How to: Add more files and remove single file when using default multiple file uploads feature for more.
EDIT - added the reason
The reason why you are getting this error is that the gem activesupport-json_encoder
(the one PR is hanging there from 2014) is no longer maintained and is not compatible with rails 5.x.
Encoding::UndefinedConversionError \xE7 from ASCII-8BIT to UTF-8
I have had this problem a lot of times, so I usually try to get rid of any characters that are invalid to UTF-8 BEFORE saving it in the Database. If you have your record saved as a String
you can replace invalid characters like so:
string = "This contains an invalid character \xE7"
string.encode('UTF-8', invalid: :replace, undef: :replace)
#=> "This contains an invalid character �"
This is ofc prior to converting it to a JSON object.
Ruby on Rails Encoding::UndefinedConversionError (\xF8 from ASCII-8BIT to UTF-8)
A simple solution was to give the File.write function the 'wb' rights
def self.convert_base64_to_file(directory: nil, file_name:, base64data:)
if directory
file_path = [directory, file_name].join('/')
else
file_path = file_name
end
file = File.open("file_name", 'w') {|f| f.write(Base64.decode64(base64data)) }
return file_path
end
I hope that'll help someone!
Related Topics
No Such File to Load -- SQLite3/Sqlite3_Native
Does Ruby Have Any Number Formatting Classes
How to Include Blank Field in Select_Tag
Check to See If an Array Is Already Sorted
Rails 4.1 Activerecord::Relation Is No More Like Array
Inherit Class-Level Instance Variables in Ruby
How to Extract a Single Character (As a String) from a Larger String in Ruby
If 'Main' Is an Instance of 'Object', Why Can't I Call It
Add Existing Classes into a Module
How to Get the Array Index or Iteration Number with an Each Iterator
How to Write to File When Using Marshal::Dump in Ruby for Object Serialization
Bootstrap Datepicker Default Value Simple_Form_For
Ruby: How to Find the Key of the Largest Value in a Hash
How to Find If Range Is Contained in an Array of Ranges
Does Ruby 1.9.2 Have an Is_A? Function