Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用UTF-8呈现JSON响应的Rails_Ruby On Rails_Ruby_Json_Encoding - Fatal编程技术网

Ruby on rails 使用UTF-8呈现JSON响应的Rails

Ruby on rails 使用UTF-8呈现JSON响应的Rails,ruby-on-rails,ruby,json,encoding,Ruby On Rails,Ruby,Json,Encoding,我正在尝试用Rails构建一个API,到目前为止做得很好,但是现在我用Umlaut添加了一个记录,我遇到了一个问题,我不能再呈现JSON了,我不知道如何解决我的问题。 以下是日志中的内容: Completed 500 Internal Server Error in 40ms JSON::GeneratorError (source sequence is illegal/malformed utf-8): app/controllers/api/v1/raids_controller.

我正在尝试用Rails构建一个API,到目前为止做得很好,但是现在我用Umlaut添加了一个记录,我遇到了一个问题,我不能再呈现JSON了,我不知道如何解决我的问题。 以下是日志中的内容:

 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)
这是我的控制器中产生这个输出的方法:

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
如果有人能帮我解决问题,那就太棒了。我已经检查过了,我确信UTF-8编码在任何地方都被使用。另外,如果我通过rails控制台检查,我会看到字符Ü被编码为名称:“\xDC”

我做到了这一点,所以在将字段保存到数据库之前格式化字段对我来说就做到了。实现这一技巧的方法是:

string.force_encoding("ISO-8859-1").encode("UTF-8")

感谢Prakash Murthy的提示。

\xDC
对utf-8无效。yopu可以尝试
“Ü”。强制编码(“ISO-8859-1”)。编码(“utf-8”)
谢谢,我知道,问题是我需要渲染整个“对象”这使得我不可能只在这上面强制编码哪一个治疗字段有这样的值?获取该字段并对其进行简单编码很可能几乎每个字段(日期除外)都有该值,因为数据来自一个用户,该用户最有可能使用德语编写,并且包含如u、ö和ä的umlauts
string.force_encoding("ISO-8859-1").encode("UTF-8")