Ruby to_json issue with error illegal/malformed utf-8
\xAE
is not a valid character in UTF-8, you have to use \u00AE
instead:
"iPhone\u00AE"
#=> "iPhone®"
Or convert it accordingly:
"iPhone\xAE".force_encoding("ISO-8859-1").encode("UTF-8")
#=> "iPhone®"
Rails rendering JSON response with UTF-8
def create
name = params[:name]
description = params[:description]
orga = params[:raidlead]
startdate = params[:startdate]
enddate = params[:enddate]
puts description
name.force_encoding("ISO-8859-1").encode("UTF-8")
description.force_encoding("ISO-8859-1").encode("UTF-8")
orga.force_encoding("ISO-8859-1").encode("UTF-8")
Did the trick, so formatting the fields before saving them in the database did the trick for me. The method that does the trick is:
string.force_encoding("ISO-8859-1").encode("UTF-8")
Thanks to Prakash Murthy for his hint.
Encoding 4 byte UTF-8 character to JSON from Rails produce invalid character
In the end, I used
JSON::dump(obj))
Related Topics
How to Make a Ruby String Safe for a Filesystem
Why Does Ruby on Rails Use Http://0.0.0.0:3000 Instead of Http://Localhost:3000
How to Spawn an Eventmachine "Inside" a Rails App
How to Do Basic Authentication with Restclient
Using Helpers in Rails 3 to Output HTML
How to Use Jquery-Tokeninput and Acts-As-Taggable-On
Fastest Way to Check If a String Matches a Regexp in Ruby
Preferred Ruby Plugin for Eclipse
Rebase Rails Migrations in a Long Running Project
What Is the Correct Way to Detect If Ruby Is Running on Windows
How to Share Variables Across My .Rb Files
Getting the Highest Value of a Column in Mongodb
How to Catch Errno::Econnreset Class in "Case When"
Rubygems Do Not Install on Os X Lion