Carrierwave を Amazon S3 で動作させることができません。エラーを取得しcan't convert nil into String
ます。
私はcouchrest_model、carrierwave 0.5.8、fog 1.9.0、Rails 3.1.11を使用しています
Rails 3.2.x に依存しているため、新しい Carrierwave にアップグレードできません
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
app/uploaders/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