0

i'm using rails3.0.10

def districts
    @names = Node.where("name like ?", "%#{params[:term]}%").limit(5).map(&:name)
    respond_to do |format|
      format.json {render :json => @names}
      format.xml {render :xml => @names}
    end
  end

render xml get right names
but render json make the names is encoded like => ["name1", "name2", "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"]
i want to get right names in their right language

any help?

Thanks in advance

M.SH
  • 357
  • 2
  • 8
  • 22

1 Answers1

0

before sending the response you must force the strings to be UTF-8

add this line for the above code

@names.map{|name| name.force_encoding('UTF-8')}

this will loop the array of names and force them to be encoded from ASCII-8BIT to UTF-8

so my method will be

def districts
  @names = Node.where("name like ?", "%#{params[:term]}%").limit(5).map(&:name)
  @names.map{|name| name.force_encoding('UTF-8')}
  respond_to do |format|
    format.json {render :json => @names}
    format.xml {render :xml => @names}
  end
end

you can also modify tour model Node like

class Node < ActiveRecord::Base
 def name
  super().force_encoding('UTF-8')
 end
end

i'm sure from this code , it works correctly
Thanks
M.SH

M.SH
  • 357
  • 2
  • 8
  • 22