2015-01-20 4 views
6

Nach dem Upgrade auf Rails 4.2 schlägt mein Code zum Speichern einer hochgeladenen Datei in der Datenbank (pg) mit der Fehlermeldung fehl: 'string enthält null Byte'.Rails 4.2: file_field, Zeichenfolge enthält Nullbytefehler

Ich habe eine neue Rails 4.2 App mit nur einem Modell und einem Binärfeld erstellt.

create_table "entries", force: :cascade do |t| 
    t.string "name" 
    t.binary "file" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
end 

Mit der Konsole:

:001 > entry = Entry.new 
=> #<Entry id: nil, name: nil, file: nil, created_at: nil, updated_at: nil> 
:002 > f = File.open('public/icon-sidesearch.png', 'rb') { |io| io.read } 
=> "\x89PNG\r\n\u001A\n\u0000\... more binary data" 
:003 > entry.file = f 
ArgumentError: string contains null byte 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/oid/bytea.rb:8:in `unescape_bytea' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/oid/bytea.rb:8:in `type_cast_from_database' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/type/binary.rb:26:in `changed_in_place?' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute.rb:54:in `changed_in_place_from?' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:74:in `attribute_changed_in_place?' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:149:in `block in changed_in_place' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:148:in `select' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:148:in `changed_in_place' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:141:in `attributes_changed_in_place' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:62:in `changed_attributes' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activemodel-4.2.0/lib/active_model/dirty.rb:173:in `attribute_changed?' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:107:in `save_changed_attribute' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:94:in `write_attribute' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods.rb:50:in `__temp__6696c656' 
    from (irb):5 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start' 
... 9 levels... 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/spring-1.2.0/lib/spring/commands/rails.rb:6:in `call' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/spring-1.2.0/lib/spring/command_wrapper.rb:38:in `call' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/spring-1.2.0/lib/spring/application.rb:183:in `block in serve' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/spring-1.2.0/lib/spring/application.rb:156:in `fork' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/spring-1.2.0/lib/spring/application.rb:156:in `serve' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/spring-1.2.0/lib/spring/application.rb:125:in `run' 
    from /Users/jose/.rvm/gems/[email protected]_sice/gems/spring-1.2.0/lib/spring/application/boot.rb:18:in `<top (required)>' 
    from /Users/jose/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
    from /Users/jose/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
    from -e:1:in `<main>' 

Irgendwelche Ideen, was passiert sein könnte?

+2

Try this: 'entry.file = Base64.encode64 (f)' – Rodrigo

+0

Dank Rodrigo. Das mache ich jetzt, um das Problem zu umgehen. Wenn ich andere Projekte anschaue, benutze ich ein _text_ Feld und kodiere/dekodiere den Inhalt der Dateien wie du es vorschreibst. – JAG

Antwort

2

Wie here erwähnt, das ist ein Rails Bug und fixiert auf 4.2.1

https://github.com/rails/rails/pull/17680

+0

Danke für diese Antwort! Genau das, was ich brauchte. –

+1

Schienen 4.2.6 und pg gem 0.18.4 mit Ruby 2.3.1 und immer noch fehlschlägt. – Juanin

+0

Sehen Sie dies auch mit einigen scheinbar normalen Saiten @Juanin haben Sie es gelöst? – MBHNYC

Verwandte Themen