2

I'm trying to build an API with Rails and been doing quite good so far, however now that I added a record with an Umlaut I come to a Problem where I can't render the JSON anymore and I can't figure out how to solve my problem. Here is what the logs say:

 Completed 500 Internal Server Error in 40ms

JSON::GeneratorError (source sequence is illegal/malformed utf-8):
  app/controllers/api/v1/raids_controller.rb:8:in `index'


  Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-   
4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.4ms)
  Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-    
4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
 Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-
4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.9ms)
 Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-
4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout     
(30.2ms)

And this is the method inside my controller that produces this output:

def index
  #This is the default call and should list all tournaments.
  @tournaments = Tournament.all
  @tournaments = @tournaments.order("startdate DESC")

  respond_to do |format|
    format.json { render :json => @tournaments }
  end
end

Would be awesome if someone could help me to solve my problem. I already checked and I'm sure that UTF-8 encoding is used everywhere. Also if I check through rails console I see that the Character Ü is encoded as name: "\xDC"

4
  • 1
    \xDC is not a valid one for utf-8 yopu can try "Ü".force_encoding("ISO-8859-1").encode("UTF-8") Commented Jan 6, 2015 at 11:17
  • Thanks, I know, the problem is that I need to render the whole "object" which doesn't make it possible for me to force encoding on just this Commented Jan 6, 2015 at 11:21
  • which of the treatment field has such value? get that field and encode it simply Commented Jan 6, 2015 at 11:42
  • well it is possible that almost every field (except dates) have this values since the data comes from a user for which will most likely will be written in german and contain umlauts like ü, ö and ä Commented Jan 6, 2015 at 11:44

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.