1

In my controller I have:

def index
    @title = 'asdsadas'
    @kategoris = Tag.where("name like ?", "%#{params[:q]}%")
    respond_to do |format|
    format.html
    format.json { render :json => @kategoris.map(&:attributes) }
    end
end

I created a monkey patch as the answer to this question: JSON encoding wrongly escaped (Rails 3, Ruby 1.9.2)

But the JSON has still not the correct encoding example:

Delta i t��vlingar f��r biljetter
Community
  • 1
  • 1
Rails beginner
  • 14,321
  • 35
  • 137
  • 257
  • hey fellow swede. encodings can be wrong in so many places. as of ruby 1.9 utf-8 is the default encoding so it is likely that you either have your data stored as something else or are telling the browser to show something else. you probably need to provide a bit more details to get an answer to this. information about all your layers and how they are configured may help. – froderik Feb 23 '12 at 21:20
  • I have the "mysql" gem and not the "mysql2" gem therefor I often need to force_encoding("UTF-8") in view. I cannot install the mysql2 gem because there is a problem with XAMPP. – Rails beginner Feb 23 '12 at 21:23
  • How do I call force_encoding("UTF-8") on the hash attributes? – Rails beginner Feb 23 '12 at 21:31
  • how is the mysql instance setup with regard to character encoding? If UTF-8 is not the default character set you may have a problem there. (I am not familiar with the mysql gems directly so I can't help you there. I always use something on top like active record or sequel.) – froderik Feb 23 '12 at 21:34

1 Answers1

2

I am pretty sure that you could fix this with your database but the quick fix may be:

new_kategoris = @kategoris.map {|v| v.force_encoding('UTF-8') }
format.json { render :json => new_kategoris.map(&:attributes) }
froderik
  • 4,642
  • 3
  • 33
  • 43
  • I get: NoMethodError in Admin::TagsController#index undefined method `each_pair' for # you are also missing the attributes. – Rails beginner Feb 23 '12 at 21:47
  • aha - easier then - I thought you got a hash back from the database (been doing too much sequel lately) answer updated – froderik Feb 23 '12 at 21:52