Rails 使用 UTF-8 呈现 JSON 响应
Rails rendering JSON response with UTF-8
我正在尝试用 Rails 构建一个 API 并且到目前为止做得很好,但是现在我添加了一个带有变音符号的记录我遇到了一个我可以的问题'我不再渲染 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 控制台检查,我会看到字符 Ü 被编码为 name: "\xDC"
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")
成功了,所以在将字段保存到数据库之前格式化字段对我来说成功了。成功的方法是:
string.force_encoding("ISO-8859-1").encode("UTF-8")
感谢 Prakash Murthy 的提示。
我正在尝试用 Rails 构建一个 API 并且到目前为止做得很好,但是现在我添加了一个带有变音符号的记录我遇到了一个我可以的问题'我不再渲染 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 控制台检查,我会看到字符 Ü 被编码为 name: "\xDC"
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")
成功了,所以在将字段保存到数据库之前格式化字段对我来说成功了。成功的方法是:
string.force_encoding("ISO-8859-1").encode("UTF-8")
感谢 Prakash Murthy 的提示。