【问题标题】:Rails error with CarrierWave and S3 on Heroku, TypeError: no implicit conversion of nil into StringHeroku上CarrierWave和S3的Rails错误,TypeError:没有将nil隐式转换为String
【发布时间】:2016-11-12 21:35:18
【问题描述】:

我无法使用 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>'

【问题讨论】:

  • 您是否设置了 ENV 密钥?
  • 是的,我使用 heroku config:set S3_BUCKET_NAME= 从 AWS 复制粘贴,其他 3 个也是如此

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


【解决方案1】:

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

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

这意味着网址根本不需要打开。

【讨论】:

    猜你喜欢
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多