How to fix ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8)

倾然丶 夕夏残阳落幕 提交于 2020-01-04 06:02:48

问题


I am getting the following error [ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8)]

Here is the log...

    Completed 500 Internal Server Error in 318ms 
    Jan 09 23:29:19 burro app/web.1:  ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8): 
    Jan 09 23:29:19 burro app/web.1:       97:         <!-- <td><%= row.notes.gsub("\n", "<br>").html_safe %></td> --> 
    Jan 09 23:29:19 burro app/web.1:       98:       </tr> 
    Jan 09 23:29:19 burro app/web.1:       99:     <% end %> 
    Jan 09 23:29:19 burro app/web.1:      100:   </tbody> 
    Jan 09 23:29:19 burro app/web.1:      101: </table> 
    Jan 09 23:29:19 burro app/web.1:      102:  

HERE IS THE CODE:

<td><%= row.notes.force_encoding("utf-8") %></td>
    <!-- <td><%= row.notes.gsub("\n", "<br>").html_safe %></td> -->
  </tr>
<% end %>

I have all these in the correct rb and erb files?

<%# encoding: utf-8 %>


config.encoding = "utf-8"

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

@data.each do |sr|
    sr.notes = sr.notes.to_s.force_encoding("UTF-8")
  end

The data in the db is encrypted so I can't run a query on my MongoDB data to see what special character is causing the issue, when displaying records?


回答1:


You can try below

config/application.rb should consist

  config.encoding = "utf8"

Also in your environments file

in config/environments/development.rb or config/environments/production.rb should have

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

and

Also use the below in .rb file

# encoding: utf-8

It tells ruby to interpret the source of the file as utf-8, even if it doesn't contain any non-ascii characters

in that particular file

hope it will solve this problem



来源:https://stackoverflow.com/questions/34702440/how-to-fix-actionviewtemplateerror-incompatible-character-encodings-ascii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!