I'm using Ruby 1.9.1 with Rails 2.3.4 My application is to handle text input
If I try something like (the inside quotation marks look different)
text = "”“"
I get the following error:
#<SyntaxError: /Users/tammam56/rubydev/favquote/lib/daemons/twitter_quotes_fetch.rb:54: invalid multibyte char (US-ASCII)
/Users/tammam56/rubydev/favquote/lib/daemons/twitter_quotes_fetch.rb:54: invalid multibyte char (US-ASCII)
/Users/tammam56/rubydev/favquote/lib/daemons/twitter_quotes_fetch.rb:54: syntax error, unexpected $end, expecting keyword_end
I need to user those quotation marks as users might input them and I have to account for that?
Any ideas?
Have you tried adding a magic comment in the script where you use non-ASCII chars? It should go on top of the script.
#!/bin/env ruby
# encoding: utf-8
It worked for me like a charm.
If you want to add magic comments on all the source files of a project easily, you can use the magic_encoding gem
sudo gem install magic_encoding
then just call magic_encoding in the terminal from the root of your app.
I just want to add my solution:
I use german umlauts like ö, ü, ä and got the same error.
#Jarek Zmudzinski just told you how it works, but here is mine:
Add this code to the top of your Controller: # encoding: UTF-8
(for example to use flash message with umlauts)
example of my Controller:
# encoding: UTF-8
class UserController < ApplicationController
Now you can use ö, ä ,ü, ß, "", etc.
That worked for me:
$ export LC_ALL=en_US.UTF-8
$ export LANG=en_US.UTF-8
Those slanted double quotes are not ASCII characters. The error message is misleading about them being 'multi-byte'.
Just a note that as of Ruby 2.0 there is no need to add # encoding: utf-8. UTF-8 is automatically detected.
Related
I got error :_ incompatible character encodings: UTF-8 and Windows-1250_
when i try to show something with chars from Poland ie. 'ąęźć'
in my application.rb i got:
config.encoding = "windows-1250"
In database.yml:
encoding: windows-1250
How can i show params in windows-1250 in rails admin panel?
I would suggest you go with utf-8 encoding (which is ruby's default these days).
Your input 'ąęźć' is a valid utf-8 string, so you would face no problem in decoding it as a utf-8 string.
If you still want to hack around, you can use:
'ąęźć'.mb_chars.tidy_bytes.to_s
which should also give you the desired output.
I ran into a problem with a Rails controller where it choked on a Unicode string:
syntax error, unexpected $end, expecting ']'
...conditions => ['url like ?', "%日本%"])
The solution to this problem was to set the encoding at the top of the controller file using
# encoding: UTF-8
Is there any way to set this globally? I keep on getting into trouble by forgetting to set it in files. Alternatively, is there a default somewhere that will make sure that all strings are thought of as Unicode? Are there any problems with setting everything to be Unicode?
In less than a month, Ruby 2.0 will be released, which will have UTF-8 as the default encoding. Then, you will not need to do that any more.
You can try setting environment variable RUBYOPT to -Ku value:
export RUBYOPT="-Ku"
Incompatible character encodings: UTF-8 and ASCII-8BIT
How can I solve this error on Rails 3.2.3 and Ruby 1.9.3?
I tried to put these two lines in the environment.rb:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
I am sure that application.rb also contains this line "config.encoding = "utf-8".
Does anyone know how to solve this?
Consider using so called magic comments on the first line of your .rb file with encoding:
# encoding: UTF-8
class Xyz
...
end
It is very important in files where you place text with accents and other non-ASCII characters. They are the primary cause of the error you mention.
Sometimes it may happen that you mistype a character and, instead of a letter, you insert a hidden symbol. Also check your file for these. Look at the line ends and in spaces.
If you have data to store on the hdd, you can try data.force_encoding('UTF-8').
I'm trying to include a degree symbol into my Rails view. If I put the degree symbol (°) straight into .html.erb file, it is displayed by browser in a normal way.
But this symbol should be transferred to view via string. And here the problem begins.
If I put this:
<%= 176.chr %>
into view, or put
.... + 176.chr
into ruby source, I get
incompatible character encodings: UTF-8 and ASCII-8BIT
How to make Rails recognize all views as UTF-8 by default?
You can use special replacement for this symbol in HTML: °.
http://www.w3schools.com/charsets/ref_html_entities_4.asp
You have to put it in HTML, outside the <%= %>. Or use raw helper. Or mark it as html_safe. And by the way, did you try to supply encoding in your chr? Like 176.chr(__ENCODING__) (__ENCODING__ here isn't placeholder, it's Ruby thing) or 176.chr(Encoding::UTF_8). All these approaches should work.
This should already be specified inside your application.rb inside /config/.
The relevant section should look like this:
module App
class Application < Rails::Application
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
end
end
I think the issue here is that you are generating a ASCII-8BIT character that should be inserted into the UTF-8 body.
If you want to use a UTF-8 String in your Ruby code you have to put this magic string into the first line of your ruby file:
# encoding: UTF-8
Details on Encoding in Ruby 1.9 can be found here
I have the following line of code in my Ruby on Rails app, which checks whether the given string contains Korean characters or not:
isKorean = !/\p{Hangul}/.match(word).nil?
It works perfectly in the console, but raises a syntax error for the actual app:
invalid character property name {Hangul}: /\p{Hangul}/
What am I missing and how can I get it to work?
This is a character encoding issue, you need to add:
# encoding: utf-8
to the top of the Ruby file you're using that regex in. You can probably use any encoding that the character class you're using exists in instead of UTF-8 if you wish. Note that in Ruby 2.0, UTF-8 is now the default, so this is not needed in Ruby 2.0+.
This is known as a "magic comment". You can and should read more about encoding in Ruby 1.9. Note that encoding in Rails views is handled automatically by config.encoding (set to UTF-8 by default in config/application.rb.
It was likely working in the console because your terminal is set to use UTF-8 already.