Carrierwave错误:无法将nil转换为String

时间:2013-06-10 10:43:47

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

我无法让carrierwave与Amazon S3配合使用。收到can't convert nil into String错误。

我正在使用couchrest_model,carrierwave 0.5.8,雾1.9.0和Rails 3.1.11

由于依赖于Rails 3.2.x

,无法升级到更高版本的载波

S3键和密码100%确定正确且有效。

初始化/ carrierwave.rb

# encoding: utf-8

require 'couchrest_model'
require 'carrierwave/validations/active_model'

module CarrierWave
  module CouchrestModel
    include CarrierWave::Mount

    ##
    # See +CarrierWave::Mount#mount_uploader+ for documentation
    #
    def mount_uploader(column, uploader, options={}, &block)
      options[:mount_on] ||= "#{column}_filename"
      property options[:mount_on]

      super

      alias_method :read_uploader, :read_attribute
      alias_method :write_uploader, :write_attribute

      include CarrierWave::Validations::ActiveModel

      validates_integrity_of  column if uploader_option(column.to_sym, :validate_integrity)
      validates_processing_of column if uploader_option(column.to_sym, :validate_processing)

      after_save "store_#{column}!".to_sym
      before_save "write_#{column}_identifier".to_sym
      after_destroy "remove_#{column}!".to_sym
    end    
  end
end

CouchRest::Model::Base.class_eval do
  extend ::CarrierWave::CouchrestModel
end

应用程序/上传/ avatar_uploader.rb

# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  storage :fog

  fog_credentials({
    :provider               => 'AWS',
    :aws_access_key_id      => 'xxx',
    :aws_secret_access_key  => 'yyy'
  })
  fog_directory  'cabify'
  fog_public     true
  fog_attributes 'Cache-Control' => 'max-age=315576000'

  def store_dir
    "#{Rails.env}/avatars/#{model.id}"
  end

  def default_url
    if model.email.present?
      model.gravatar_url
    else
      "/images/avatars/" + [version_name, "default.png"].compact.join('_')
    end
  end

  process :resize_to_fit => [250, 250]

  version :thumb do
    process :resize_to_fill => [80, 80]
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

宝石中有错误的行:

https://github.com/carrierwaveuploader/carrierwave/blob/v0.5.8/lib/carrierwave/uploader/url.rb#L18

问题是root == nil

回溯

TypeError - can't convert nil into String:
  (gem) carrierwave-0.5.8/lib/carrierwave/uploader/url.rb:22:in `url'
  (gem) carrierwave-0.5.8/lib/carrierwave/uploader/versions.rb:164:in `url'
  (gem) carrierwave-0.5.8/lib/carrierwave/uploader/default_url.rb:8:in `url'
  app/models/user.rb:344:in `avatar_url'
  app/controllers/admin/users_controller.rb:56:in `update'
  (gem) actionpack-3.1.11/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  (gem) actionpack-3.1.11/lib/abstract_controller/base.rb:167:in `process_action'
  (gem) actionpack-3.1.11/lib/action_controller/metal/rendering.rb:10:in `process_action'
  (gem) actionpack-3.1.11/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  (gem) activesupport-3.1.11/lib/active_support/callbacks.rb:443:in `_run__2850817817751956370__process_action__3254483072592469464__callbacks'
  (gem) activesupport-3.1.11/lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
  (gem) activesupport-3.1.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
  (gem) actionpack-3.1.11/lib/abstract_controller/callbacks.rb:17:in `process_action'
  (gem) actionpack-3.1.11/lib/action_controller/metal/rescue.rb:17:in `process_action'

app / models / user.rb(第344行)

class User < CouchRest::Model::Base
  mount_uploader :avatar, AvatarUploader

  def avatar_url
    avatar.thumb.url
  end
end

上传表格

= form_for [:admin, @user], :html => {:multipart => true} do |f|
  = f.file_field :avatar

2 个答案:

答案 0 :(得分:1)

在斯蒂芬·默多克给出的this branch解决方案对我有帮助。尝试添加


def root
  Rails.root.join 'public/'
end

在您的上传器中

强制CarrierWave临时上传目录位置。

答案 1 :(得分:0)

最后,我通过分配gem并将依赖项更改为Rails 3.1来修复它。因此,您可以使用Rails 3.1

升级到carrierwave 0.8.0

如果有人遇到同样的问题,请试试我的前叉:https://github.com/michaelkoper/carrierwave.git