为什么carrierwave会保存整个模型的副本?

时间:2013-08-03 19:04:00

标签: ruby-on-rails ruby ruby-on-rails-3 carrierwave

我一直在使用CarrierWave与AWS永远没有问题,但是在我的最新项目中,我根本无法使用它。文件上传到AWS没有任何问题,但是它不是保存数据库表中的文件位置,而是存储整个模型的副本。当页面加载时,我收到错误,因为rails无法弄清楚如何处理表中存储的数据 请查看以下..直接从rails控制台,其中profile_picture是使用挂载到CarrierWave的字段。

它似乎保存整个对象而不是生成的URL。有没有人对此有任何想法?

dog = Dog.first
=> #<Dog id: 2, user_id: nil, name: "Lentil", profile_picture: "971935_556519314400437_65392555_n.jpg", breed_id: 2, 
color: "fawn with black mask", age: 1, gender: "m", created_at: "2013-08-03 17:57:00", updated_at: "2013-08-03 18:12:33">

dog.profile_picture
=> #<ImageUploader:0x007f83388b70f0 @model=#<Dog id: 2, user_id: nil, name: "Lentil", profile_picture: "971935_556519314400437_65392555_n.jpg", breed_id: 2, color: "fawn with black mask", age: 1, gender: "m", created_at: "2013-08-03 17:57:00", updated_at: "2013-08-03 18:12:33">, @mounted_as=:profile_picture, @storage=#
<CarrierWave::Storage::Fog:0x007f83388b6b50 @uploader=#<ImageUploader:0x007f83388b70f0 ...>, @connection=#<Fog::Storage::AWS::Real:0x007f8336d5e3d0 @aws_access_key_id="xxx", @aws_secret_access_key="xxx", @hmac=#<Fog::HMAC:0x007f8336d5e100 @key="xxx", @digest=#<OpenSSL::Digest::Digest: xxx>, @signer=#<Proc:0x007f8336d5ddb8@/app/vendor/bundle/ruby/2.0.0/gems/fog-0.9.0/lib/fog/core/hmac.rb:22 
(lambda)>>, @endpoint=nil, @host="s3.amazonaws.com", @path="/", @port=443, @scheme="https", @connection=#<Fog::Connection:0x007f8336d5db10 @excon=#<Excon::Connection:0x007f8336d5d8b8 @connection={:headers=>{}, :host=>"s3.amazonaws.com", :mock=>nil, :path=>"/", :port=>"443", :query=>nil, :scheme=>"https"}, @socket_key="s3.amazonaws.com:443">, @persistent=true>>>, @file=#<CarrierWave::Storage::Fog::File:0x007f83388b6510 @uploader=#<ImageUploader:0x007f83388b70f0 
...>, @base=#<CarrierWave::Storage::Fog:0x007f83388b6b50 @uploader=#<ImageUploader:0x007f83388b70f0 ...>, @connection=#<Fog::Storage::AWS::Real:0x007f8336d5e3d0 @aws_access_key_id="xxx", @aws_secret_access_key="xxx", @hmac=#<Fog::HMAC:0x007f8336d5e100 @key="xxx", @digest=#<OpenSSL::Digest::Digest: xxx>, @signer=#<Proc:0x007f8336d5ddb8@/app/vendor/bundle/ruby/2.0.0/gems/fog-0.9.0/lib/fog/core/hmac.rb:22 
(lambda)>>, @endpoint=nil, @host="s3.amazonaws.com", @path="/", @port=443, @scheme="https", @connection=#<Fog::Connection:0x007f8336d5db10 @excon=#<Excon::Connection:0x007f8336d5d8b8 @connection={:headers=>{}, :host=>"s3.amazonaws.com", :mock=>nil, :path=>"/", :port=>"443", :query=>nil, :scheme=>"https"}, @socket_key="s3.amazonaws.com:443">, @persistent=true>>>, @path="uploads/dog/profile_picture/2/971935_556519314400437_65392555_n.jpg">, @versions={}>

2 个答案:

答案 0 :(得分:1)

宝石出现问题..重新安装后,一切正常。

答案 1 :(得分:0)

Carrierwave会在安装时覆盖getter,因此当您调用profile_picture时,它将返回上传器的实例而不是文件URL。在实际数据库中,仅存储文件URL。

您可以在上传器上调用许多不同的方法。最常见的是url。在使用gem之前,您应该阅读Github上的Carrierwave文档,以避免进一步混淆。