I have a controller index method that renders html and json:
def index
@articles = Article.all
respond_to do |format|
format.html
format.json { render text: @articles.to_json }
end
end
In my html, everything works fine, names are displayed as Müller
(with a german umlaut). However, when I render json, I get weird characters like M\u00fcller
.
When I look at the encoding of the title
attribute in my Article
model, it returns UTF-8
:
puts @articles.first.attributes["title"]
=> "Müller"
puts @articles.first.attributes["title"].encoding
=> #<Encoding:UTF-8>
But when I convert it into json, I get wrong characters:
puts @articles.first.attributes.to_json
=> "{\"id\":293,\"title\":\"M\\u00fcller\"}"
I'm not sure why this only happens for json. I'm using Rails 3.2.9.