轨道omniauth和UTF-8错误 [英] rails omniauth and UTF-8 errors
问题描述
Encoding :: CompatibilityError:不兼容的字符编码:
ASCII-8BIT和UTF-8
omniauth=>
{user_info=>
{name=>JoeMcÙisnean,
last_name=>McÙisnean,
first_name=>Joe,
email =>someemail@gmail.com},
uid=>
https://www.google.com/accounts/o8/id?id=AItOawnQmfdfsdfsdfdsfsdhGWmuLTiX2Id40k,
provider=>google_apps}
在我的用户模型中
def apply_omniauth omniauth)
#add关于用户的一些信息
self.email = omniauth ['user_info'] ['email']如果email.blank?
self.name = omniauth ['user_info'] ['name']如果name.blank?
self.name = omniauth ['user_info'] [:name] if name.blank?
self.nickname = omniauth ['user_info'] ['nickname'] if nickname.blank?
self.nickname = name.gsub('','')。如果昵称是
除非omniauth ['credentials']。空白?
user_tokens.build(:provider => omniauth ['provider'],
:uid => omniauth ['uid'],
:token => omniauth ['credentials' ] ['token'],
:secret => omniauth ['credentials'] ['secret'])
else
user_tokens.build(:provider => omniauth ['provider '],:uid => omniauth ['uid'])
end
end
$ b $我不太熟悉UTF编码,所以我不知道我应该在哪里指定编码?但是我猜这是在它被放入用户模型并创建之前,我不确定该怎么做?
更新:
Rails 3.0.10
Omniauth 0.2.6
Ruby 1.9.2
PG 0.11.0
默认编码是UTF-8
似乎没有这样做,所以我进一步挖掘,发现这个视图:
显示/Users/holden/Code/someapp/app/views/users/registrations/_signup.html.erb第5行提高的位置:
不兼容的字符编码:ASCII-8BIT和UTF-8
提取源(绕行#5):
2:<%= f.error_messages%>
3:
4:<%= f.input:name,:hint => '你的真名'%>
5:<%= f.input:昵称,:hint => '您选择的用户名'%>
6:
7:<%除非@ user.errors [:email] .present?或@ user.email%>
8:<%= f.input:email,:as => :hidden%>
更新更新:
作为返回ASCII-8BIT字符的omniauth宝石,所以我的下一个问题是如何解析散列并将其转换回UTF8,以便我的应用程序不会爆炸?
session [:omniauth] = omniauth.to_utf8
这个疯狂骑行的另一部分是当我输入这进入控制台
d = {user_info=> {email=>someemail@gmail.com ,first_name=>Joe,last_name=>Mc\xC3\x99isnean,name=>Joe Mc\xC3\x99isnean}}
它会自动将其转换为UTF-8,但在推入会话时会爆炸
=> {user_info=> {email=>someemail@gmail.com,first_name="Joe,last_name=>McÙisnean,name=& McÙisnean}}
如果有的话,这是一个痛苦的噩梦。
Omniauth被证明是产生ASCII-8BIT的问题
我最终强迫Omniauth哈希转换为提交使用:
omniauth_controller.rb
session [ :omniauth] = omniauth.to_utf8
添加递归方法强制将流氓ASCII-8BIT转换为UTF8
some_initializer.rb
class Hash
def to_utf8
哈希[
self.collect do | k,v |
if(v.respond_to?(:to_utf8))
[k,v.to_utf8]
elsif(v.respond_to?(:encoding))
[k,v。 dup.force_encode('UTF-8')]
else
[k,v]
end
end
]
end
end
特别感谢tadman
I had a recent error using omniauth trying to populate some fields from Google's login
Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
"omniauth"=>
{"user_info"=>
{"name"=>"Joe McÙisnean",
"last_name"=>"McÙisnean",
"first_name"=>"Joe",
"email"=>"someemail@gmail.com"},
"uid"=>
"https://www.google.com/accounts/o8/id?id=AItOawnQmfdfsdfsdfdsfsdhGWmuLTiX2Id40k",
"provider"=>"google_apps"}
In my user model
def apply_omniauth(omniauth)
#add some info about the user
self.email = omniauth['user_info']['email'] if email.blank?
self.name = omniauth['user_info']['name'] if name.blank?
self.name = omniauth['user_info'][:name] if name.blank?
self.nickname = omniauth['user_info']['nickname'] if nickname.blank?
self.nickname = name.gsub(' ','').downcase if nickname.blank?
unless omniauth['credentials'].blank?
user_tokens.build(:provider => omniauth['provider'],
:uid => omniauth['uid'],
:token => omniauth['credentials']['token'],
:secret => omniauth['credentials']['secret'])
else
user_tokens.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
end
end
I'm not hugely knowledgeable about UTF encoding, so I'm not sure where I should be specifying the encoding? But I'm guessing it's here before it get's put into the user model and created, I'm unsure what to do about it?
UPDATE:
Rails 3.0.10 Omniauth 0.2.6 Ruby 1.9.2 PG 0.11.0
Default encoding is UTF-8
That didn't seem to be it, so I dug further and found this in the view:
Showing /Users/holden/Code/someapp/app/views/users/registrations/_signup.html.erb where line #5 raised:
incompatible character encodings: ASCII-8BIT and UTF-8
Extracted source (around line #5):
2: <%= f.error_messages %>
3:
4: <%= f.input :name, :hint => 'your real name' %>
5: <%= f.input :nickname, :hint => 'Username of your choosing' %>
6:
7: <% unless @user.errors[:email].present? or @user.email %>
8: <%= f.input :email, :as => :hidden %>
UPDATE UPDATE:
It seems to be the omniauth gem which is returns the ASCII-8BIT chars, so my next question is how can I parse the hash and convert it back into UTF8 so my app doesn't explode?
session[:omniauth] = omniauth.to_utf8
Another part to this crazy ride is when I type this into the console
d={"user_info"=>{"email"=>"someemail@gmail.com", "first_name"=>"Joe", "last_name"=>"Mc\xC3\x99isnean", "name"=>"Joe Mc\xC3\x99isnean"}}
It automatically converts it to UTF-8, but it explodes when shoved into a session
=> {"user_info"=>{"email"=>"someemail@gmail.com", "first_name"=>"Joe", "last_name"=>"McÙisnean", "name"=>"Joe McÙisnean"}}
This is a painful nightmare if there ever was one.
Omniauth proved to be the problem producing the ASCII-8BIT
I ended up forcing the Omniauth hash into submission using:
omniauth_controller.rb
session[:omniauth] = omniauth.to_utf8
added recursive method to force convert the rogue ASCII-8BIT to UTF8
some_initializer.rb
class Hash
def to_utf8
Hash[
self.collect do |k, v|
if (v.respond_to?(:to_utf8))
[ k, v.to_utf8 ]
elsif (v.respond_to?(:encoding))
[ k, v.dup.force_encode('UTF-8') ]
else
[ k, v ]
end
end
]
end
end
Special thanks to tadman
recursively convert hash containing non-UTF chars to UTF
这篇关于轨道omniauth和UTF-8错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!