I am using rails 2.3.3 and ruby 1.9.1.
I am trying to render a view that includes a partial. In the partial i output a field of a model that is encoded in UTF8.
This fails with
ActionView::TemplateError (incompatible character encodings: ASCII-8BIT and UTF-8) on line #248 of app/views/movie/show.html.erb:
245: <!-- Coloumn right | start -->
246: <div class="col_right">
247:
248: <%= render :partial => 'movie_stats' %>
249:
250: <!-- uploaders -->
251: <div class="box_white">
On the other hand, i can output the field with utf8 content just fine if i directly use that field in a view (when it is not in a partial).
How can i fix this?
I already tried setting the default encoding but that did not seem to work.
I just had this as well so I think its worth having the correct answer.
The 2.8.1 MySql gem is not utf-8 friendly, so it sometimes will return UTF strings and lie to Rails, telling it that they are ASCII when in fact they are UTF-8. This makes things explode.
So: you can either monkey patch or get a compatible MySql gem. See: http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/
There appears to be an issue with ERB's encoding in Ruby 1.9. More details are in this Lighthouse ticket. A patch with a workaround has been included, perhaps it works for you?
The problem is erb code in ruby 1.9 distribution. When it compiles the template code it forces a 'ASCII-8bit' encoding, the problem is when the template code has multibyte characters the template code is returned in a 'ASCII-8bit' string and when this string is concat with a 'UTF8' string with multibyte character the exception is raised because the strings between this encodings are only compatible when both only have seven-bit characters.
There seems to be an incompatibility between Ruby 1.9x and the mysql gem with regard to how strings are passed back and forth (specifically the encoding of the strings).
To fix, run
gem install mysql2
on the server and update the database configuration file to use this gem instead of the previous one.
Related
A basic task: evaluate field's value and show warning if results are not satisfying. The actual code doesn't matter, since it works perfectly until I actually change sample warning's test to the one, which should actually be there, which throws this accursed error at me:
Error: Encoding::UndefinedConversionError: U+0417 from UTF-8 to ISO-8859-1
In any other case I would've used i18n or magic comment, but neither seem to work here, since apparently you can't mix ruby into .coffee file. Is there any way to avoid this without resorting to putting unnecessary javascript into views?
You can use ruby in coffeescript assets. Just rename the file to .js.erb.coffee and use good old <%= ruby_code %>.
<a class="close" href="#">×</a>
I get an error regarding the use of ×.
It's used in error messages on twitter's bootstrap framework, I get an invalid byte sequence in UTF-8 error when I try to use it. Is there any work-around? Apart from using a normal x or X.
I have:
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
In my application.rb
This seems almost too simple, but why aren't you using ×?
You need to set the encoding at the top of the file where that character is used. You can do this with:
# coding: utf-8
class MyClass
end
I haven't tried it in an erb file, but I don't see why that would be any different. I think you can use the word "encoding" too instead of just "coding" if that feels better. All that is required is at minimum "coding".
What editor are you using?
I suspect that you are saving the source file using an encoding other than UTF-8 (such as Latin-1 or ANSI on Windows), which is then causing ruby to fail to interpret the file correctly.
I've tried adding the times symbol to one of my views (using HAML) and it worked correctly. I'm using VIM as my editor and saving in UTF-8 without any BOM.
#encoding: utf-8
class ClassiClass
end
everything works fine!
I'm using rails 3.1 and the asset pipeline (ruby 1.9.2).
I get the following error when trying to serve a javascript js.erb file that has utf-8 encoded strings
invalid byte sequence in US-ASCII
I've set Encoding.default_external = "UTF-8" in my environment.rb file. How do i get the asset pipeline to serve with a different encoding?
EDIT
The error only shows up when I'm generating the utf-8 character outside of the file (in this case by querying from the DB). The error goes away if I add
<% "日" %>
to the top of the file. I'm guessing there's some kind of encoding guessing going on here, but how do I avoid it without that hacky solution?
When loading a file, Ruby tries to "guess" its encoding. If no UTF-8 or any other non-ASCII characters are found, it uses US-ASCII as encoding for the file and throws an error if it suddenly encounters a non-ASCII character, which e.g. is loaded at run-time.
The best solution for this problem is to force Ruby to use a certain encoding by adding
# encoding: utf-8 as the first line of a .rb file or <%# encoding: utf-8 %> if it's a .erb file.
WIth ruby 1.8.7 and rails 3.0.3 on Mac OS I always got "&#...;" characters in ERB template output running rspec 2.4 controllers tests (with integrate_views).
Besides "&" character and Cyrillic characters are always escaped, even using <%= raw '...' or html_safe methods.
Can anybody give a clue - what's going on here?
You mean console/terminal output?
Try editing you ~/.profile file and pasting this line:
export LANG=sr_YU.UTF-8
You can find listing of all available locales here.
Replace sr_YU with your own cyrillic locale.
I'm getting the following error with my Ruby 1.9 & Rails 2.3.4. This happens when user submits a non-ASCII standard character.
I read a lot of online resources but none seems to have a solution that worked.
I tried using (as some resources suggested)
string.force_encoding('utf-8')
but it didn't help.
Any ideas how to resolve this? Is there a way to eliminate such characters before saving to the DB? Or, is a there a way to make them show?
For ruby 1.9 and Rails 3.0.x, use the mysql2 adapter.
In your gemfile:
gem 'mysql2', '~> 0.2.7'
and update your database.yml to:
adapter: mysql2
http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/
I don't know much about Ruby (or Rails), but I imagine the problem is caused by a lack of control over your character encodings.
First, you should decide which encoding you're storing in your database. Then, you need to make sure to convert all text to that encoding before storing in the database. In order to do that, you first need to know which encoding it is to begin with.
One often repeated piece of advice is to decode all input from whatever encoding it uses, to unicode (if your language supports it) as soon as possible after you get control of it. Then you know that all the text you handle in your program is unicode. On the other end, encode the text to whatever output-encoding you want as a last step before outputting it.
The key is to always know which encoding a piece of text is using at any given place in your code.