未定义的方法`content_length'为零:NilClass雾存储[carrierwave]

时间:2014-02-18 12:10:17

标签: ruby-on-rails carrierwave

我有两个模型在带有carrierwave的rails项目中使用相同的上传器

class Brand < ActiveRecord::Base
    mount :avatar, AvatarUploader
end

class Profile < ActiveRecord::Base
    mount :avatar, AvatarUploader
end

然后当我尝试将一个图像复制到另一个模型时

profile.avatar = brand.avatar
profile.save!

使用以下stacktrace

失败
undefined method `content_length' for nil:NilClass
usr/lib64/ruby/gems/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:238:in `size'
/usr/lib64/ruby/gems/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/proxy.rb:57:in `size'
/usr/lib64/ruby/gems/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/sanitized_file.rb:95:in `size'
/usr/lib64/ruby/gems/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/sanitized_file.rb:135:in `empty?'
/usr/lib64/ruby/gems/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/cache.rb:119:in `cache!'
/usr/lib64/ruby/gems/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/mount.rb:327:in `cache'
/usr/lib64/ruby/gems/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/mount.rb:179:in `avatar='
/usr/lib64/ruby/gems/1.9.1/gems/carrierwave-0.9.0/lib/carrierwave/orm/activerecord.rb:38:in `avatar='
/usr/share/nginx/cranberrychic/releases/20140213171925/app/services/brand_into_user_converter.rb:42:in `copy_avatar'
/usr/share/nginx/cranberrychic/releases/20140213171925/app/services/brand_into_user_converter.rb:13:in `convert'
(irb):64:in `block (2 levels) in irb_binding'
/usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
/usr/lib64/ruby/gems/1.9.1/gems/newrelic_rpm-3.6.0.78/lib/new_relic/agent/method_tracer.rb:486:in `block in transaction_with_trace_ActiveRecord_self_name_transaction'
/usr/lib64/ruby/gems/1.9.1/gems/newrelic_rpm-3.6.0.78/lib/new_relic/agent/method_tracer.rb:235:in `trace_execution_scoped'
/usr/lib64/ruby/gems/1.9.1/gems/newrelic_rpm-3.6.0.78/lib/new_relic/agent/method_tracer.rb:481:in `transaction_with_trace_ActiveRecord_self_name_transaction'
(irb):62:in `block in irb_binding'
/usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation/delegation.rb:6:in `each'
/usr/lib64/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation/delegation.rb:6:in `each'
(irb):59:in `irb_binding'
/usr/lib64/ruby/1.9.1/irb/workspace.rb:80:in `eval'
/usr/lib64/ruby/1.9.1/irb/workspace.rb:80:in `evaluate'
/usr/lib64/ruby/1.9.1/irb/context.rb:254:in `evaluate'
/usr/lib64/ruby/1.9.1/irb.rb:159:in `block (2 levels) in eval_input'
/usr/lib64/ruby/1.9.1/irb.rb:273:in `signal_status'
/usr/lib64/ruby/1.9.1/irb.rb:156:in `block in eval_input'
/usr/lib64/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement'
/usr/lib64/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
/usr/lib64/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
/usr/lib64/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
/usr/lib64/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
/usr/lib64/ruby/1.9.1/irb.rb:155:in `eval_input'
/usr/lib64/ruby/1.9.1/irb.rb:70:in `block in start'
/usr/lib64/ruby/1.9.1/irb.rb:69:in `catch'
/usr/lib64/ruby/1.9.1/irb.rb:69:in `start'
/usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
/usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
/usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'

对我而言是一个带有雾存储的载波的错误,因为在使用文件存储进行开发时,一切都按预期工作。当我尝试使用carrierwave以任何可能的方式将图像从模型复制到另一个模型时,它会以相同的错误失败

我的carrierwave版本是0.9.0

和rails版本是3.2.13和ruby 1.9.3-p327

1 个答案:

答案 0 :(得分:2)

我相信你做错了

profile.avatar = brand.avatar

无效,因为profile.avatar=需要文件对象而brand.avatar不会为您提供文件对象(它会返回一个上传者,即头像对象)

我相信你正在寻找这种方法

profile.remote_avatar_url = brand.avatar.url
profile.save!

考虑brand.avatar.url给你一个url,其中载波可以从哪里下载文件

有关remote_{uploader}_url的更多信息也可以在Carrierwave Readme

中找到

希望这个帮助