How to fix ActionView::Template::Error (incompatib

2019-07-09 14:12发布

问题:

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