I there, I'm new 开发者_如何学Cto ruby (and rails) and having som problems when using Swedish letters in strings. In my action a create a instance variable like this:
@title = "Välkommen"
And I get the following error:
invalid multibyte char (US-ASCII)
syntax error, unexpected $end, expecting keyword_end
@title = "Välkommen"
^
What's happening?
EDIT: If I add:
# coding: utf-8
at the top of my controller it works. Why is that and how can I slove this "issue"?
See Joel spolsky's article "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)".
To quote the part that answers this questions concisely
The Single Most Important Fact About Encodings
If you completely forget everything I just explained, please remember one extremely important fact. It does not make sense to have a string without knowing what encoding it uses. You can no longer stick your head in the sand and pretend that "plain" text is ASCII.
This is why you must tell ruby what encoding is used in your file. Since the encoding is not marked in some sort of metadata associated with your file, some software assumed ASCII until it knows better. Ruby 1.9 probably does so until your comment when it will stop, and restart reading the file now decoding it as utf-8.
Obviously, if you used some other Unicode encoding or some more local encoding for your ruby file, you would need to change the comment to indicate the correct encoding.
The "magic comment" in Ruby 1.9 (on which Rails 3 is based) tells the interpreter what encoding to expect. It is important because in Ruby 1.9, every string has an encoding. Prior to 1.9, every string was just a sequence of bytes.
A very good description of the issue is in James Gray's series of blog posts on Ruby and Unicode. The one that is exactly relevant to your question is http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings (but see the others because they are very good).
The important line from the article:
The first is the main rule of source Encodings: source files receive a US-ASCII Encoding, unless you say otherwise.
There are several places that can cause problems with utf-8 encoding. but some tricks are to solve this problem:
- make sure that every file in your project is utf-8 based (if you are using rad rails, this is simple to accomplish: mark your project, select properties, in the "text-file-encoding" box, select "other: utf-8")
Be sure to put in your strange "å,ä,ö" characters in your files again or you'll get a mysql error, because it will change your "å,ä,ö" to a "square" (unknown character)
in your databases.yml set for each server environment (in this example "development" with mysql)
development: adapter: mysql encoding: utf8
set a before filter in your application controller (application.rb):
class ApplicationController < ActionController::Base before_filter :set_charset def set_charset @headers["Content-Type"] = "text/html; charset=utf-8" end end
be sure to set the encoding to utf-8 in your mysql (I've only used mysql.. so I don't know about other databases) for every table. If you use mySQL Administrator you can do like this: edit table, press the "table option" tab, change charset to "utf8" and collation to "utf8_general_ci" ( Courtsey : kombatsanta )
精彩评论