Heroku上的CarrierWave和S3出现Rails错误,TypeError:没有将nil隐式转换为String

时间:2016-07-11 02:46:24

标签: ruby-on-rails heroku amazon-s3 carrierwave

我无法使用CarrierWave和S3上传图片。我正在使用这些宝石:

gem 'carrierwave'
gem 'carrierwave-aws'

我在代码,环境变量和AWS上正确设置了配置:

CarrierWave.configure do |config|
  config.storage    = :aws
  config.aws_bucket = ENV["S3_BUCKET_NAME"]
  config.aws_acl    = 'public-read'
  config.aws_credentials = {
    access_key_id:     ENV["AWS_ACCESS_KEY_ID"],
    secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
    region:            ENV["AWS_REGION"]
  }
  config.cache_dir = "#{Rails.root}/tmp/uploads"
end

class ImageUploader < CarrierWave::Uploader::Base
  storage :aws

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def cache_dir
    "#{Rails.root}/tmp/uploads"
  end
end

一位消息人士表示要在上传器中专门为Heroku包含cache_dir行,但它似乎没有效果:

https://github.com/carrierwaveuploader/carrierwave/wiki/how-to%3A-make-carrierwave-work-on-heroku

以下是保存图像的一些标准代码,但它会出错:

> url = "https://dl.dropboxusercontent.com/u/22125572/Kira_opt.jpg"
> image = open(url)
> user = User.first
> user.image = image
TypeError: no implicit conversion of nil into String
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/cache.rb:171:in `join'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/cache.rb:171:in `cache_path'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/cache.rb:143:in `block in cache!'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/cache.rb:134:in `cache!'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/mount.rb:329:in `cache'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/mount.rb:163:in `image='
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/orm/activerecord.rb:39:in `image='
from (irb):4
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'

1 个答案:

答案 0 :(得分:0)

看起来我应该保存远程图像的方式是:

url = "https://dl.dropboxusercontent.com/u/22125572/Kira_opt.jpg"
user = User.first
user.remote_image_url = url
user.save

表示根本不需要打开网址。