gpt4 book ai didi

ruby-on-rails - Rails 4.2 : file_field, 字符串包含空字节错误

转载 作者:行者123 更新时间:2023-12-04 15:43:40 26 4
gpt4 key购买 nike

升级到 Rails 4.2 后,我将上传的文件保存到数据库 ( pg ) 的代码失败并显示错误消息:“字符串包含空字节”。

我创建了一个只有一个模型和一个二进制字段的新 Rails 4.2 应用程序。

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

使用控制台:
: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/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/oid/bytea.rb:8:in `unescape_bytea'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_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/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/type/binary.rb:26:in `changed_in_place?'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute.rb:54:in `changed_in_place_from?'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:74:in `attribute_changed_in_place?'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_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/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:148:in `select'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:148:in `changed_in_place'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:141:in `attributes_changed_in_place'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:62:in `changed_attributes'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activemodel-4.2.0/lib/active_model/dirty.rb:173:in `attribute_changed?'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:107:in `save_changed_attribute'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:94:in `write_attribute'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods.rb:50:in `__temp__6696c656'
from (irb):5
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
... 9 levels...
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/commands/rails.rb:6:in `call'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/command_wrapper.rb:38:in `call'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:183:in `block in serve'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:156:in `fork'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:156:in `serve'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:125:in `run'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_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>'

任何想法可能会发生什么?

最佳答案

如前所述 here ,这是一个 Rails 错误并在 4.2.1 上修复

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

关于ruby-on-rails - Rails 4.2 : file_field, 字符串包含空字节错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28042876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com