2011-10-07 45 views
0

我已經運行我的程序,一切正常。我現在試圖寫一些測試,我一直得到這個錯誤:Rails 3中的編碼錯誤

Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8 

我沒有使用任何奇怪的字符,據我所知。我什至不知道從哪裏開始這個錯誤。我試圖將這兩行添加到environment.rb文件中:

Encoding.default_internal = Encoding::UTF_8 
Encoding.default_external = Encoding::UTF_8 

並且沒有幫助。下面是測試我試圖運行:

require 'test_helper' 

class UserTest < ActiveSupport::TestCase 
    test "should require all fields" do 
    u = User.new 
    assert_false u.valid? 
    end 
end 

這裏是我的users.yml裏的文件:

one: 
    name: MyString 
    password: MyString 

two: 
    name: MyString 
    password: MyString 

<% (1..5).each do |i| %> 
    user_<%= i %>: 
    name: user_<%= i %> 
    password: user_<%= i %> 
<% end %> 

這裏是我的堆棧跟蹤:

Creating sqlite :memory: database 
-- create_table("comment_votes", {:force=>true}) 
    -> 0.0021s 
-- create_table("comments", {:force=>true}) 
    -> 0.0013s 
-- add_index("comments", ["post_id"], {:name=>"index_comments_on_post_id"}) 
    -> 0.0005s 
-- create_table("posts", {:force=>true}) 
    -> 0.0015s 
-- create_table("users", {:force=>true}) 
    -> 0.0013s 
-- create_table("votes", {:force=>true}) 
    -> 0.0010s 
-- initialize_schema_migrations_table() 
    -> 0.0009s 
-- assume_migrated_upto_version(20111005200722, ["db/migrate"]) 
    -> 0.0013s 

Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8 

1測試,0斷言,0失敗,1錯誤,0下降,0遺漏,0通知

測試套件完成:0.058747秒

以退出碼1結束的程序

回答

0

您的密碼是否被鹽漬?並且鹽是否會使用不符合UTF-8或US-ASCII的隨機字節?這已經讓我感覺到了。我必須在我的密碼鹽上輸入force_encoding("BINARY")

+0

是的,我的密碼是鹽漬的。你把代碼放在哪裏? – user972276

+0

那麼,我用DataMapper,而不是ActiveRecord,所以在我的情況下,它實際上是一個DM中的錯誤,我[提交了補丁](https://github.com/datamapper/dm-core/pull/141)。也許嘗試定義'password_salt ='(或任何字段)和'password_salt'並在那裏做? – d11wtq

+0

你可以發佈你的密碼salting和檢查邏輯?那麼我可能會幫助更多。 – d11wtq