开发者

Rails 3.1, Ruby 1.9.2-p180 and UTF-8 issues

I'm having some trouble 开发者_开发问答with UTF-8 characters. Here's db/seeds.rb

User.create(username: 'eml', first_name: '****', last_name: '****äck', 
            email: 'somemail@example.com', password: 'asdasd')

My terminal (OSX 10.5.8) is set to use UTF-8, Rails (according to application.rb) is set to use utf-8. Here's the error:

$ rake db:seed
rake aborted!
/Projects/***/db/seeds.rb:8: invalid multibyte char (US-ASCII)
/Projects/***/db/seeds.rb:8: invalid multibyte char (US-ASCII)
/Projects/***/db/seeds.rb:8: syntax error, unexpected $end, expecting ')'
...ame: '****', last_name: '****äck', 
...                               ^

Tasks: TOP => db:seed
(See full trace by running task with --trace)


Add this line # encoding: utf-8 on the top of seeds.rb file and any ruby file use spécials chars

I recommed you to adding Encoding.default_external = "UTF-8" to config.ru file and in config/environment.rb for encoding templating

And finally you can add "".force_encoding('UTF-8) to your string that pose problems.

Update :

Add full line on config.ru :

# Not override -E option or LANG and apply only for ruby > 1.9
if Object.const_defined?(:Encoding) && !ENV['LANG'] && !ENV['RUBYOPT'].include?('-E')
Encoding.default_internal = 'utf-8'
Encoding.default_external = 'utf-8'
end

Source

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜