我想为Carrier wave Direct配置一个上传路径,我用它来直接将文件上传到亚马逊s3。在app / uploaders / myuploader.rb中,我有,
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
我想修改它,以便路径看起来像,
“上传/#{model.class.to_s.underscore} /#{mounted_as} / {#} User.name”。
所以这将是一些事情,
“上传/用户/化身/ michael_jordan / ..”
用户是安装上传器的型号。
我已经浏览了github(https://github.com/dwilkie/carrierwave_direct)中Carrier wave Gem的源代码,我无法弄清楚如何将值从活动模型对象传递给上传器来创建上传路径(关键字)为亚马逊s3)。有没有办法做到这一点?
谢谢!
龙
答案 0 :(得分:5)
好的。一个月后我被迫处理同样的问题,这次我把它钉在了一边。在这里提出我的解决方案,这样可以帮助其他人解决同样的问题, 1)覆盖store_dir,使其返回空字符串
def store_dir
""
end
2)到你的控制器并将你喜欢的键分配给上传者对象的“key”属性,如
@uploader = @player.avatar
@uploader.key =@player.name +"/\${filename}"
#say if player name in is jordan and the filename is jordan.jpg.
#the uplaod path now will be
#http://bucketname/jordan/jordan.jpg.
这里播放器是模型对象,avatar是安装上传器的属性,键将设置为播放器名称/文件名。
我正在寻找的是什么:)
答案 1 :(得分:2)
如果model.name
不起作用,您可以尝试使用#read_attribute方法直接访问名称字段
所以你的store_dir方法看起来像
def store_dir
uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.send(:name)}
end
但我相信model.name
应该有用
答案 2 :(得分:0)
第一个提供的解决方案对我不起作用。在对这整天进行故障排除后,我检查了动态创建密钥的代码:
https://github.com/dwilkie/carrierwave_direct/blob/master/lib/carrierwave_direct/uploader.rb
# as defined in carrierwave_direct/uploader.rb
def key
return @key if @key.present?
if present?
self.key = URI.parse(URI.encode(url, " []+()")).path[1 .. -1] # explicitly set key
else
@key = "#{store_dir}/#{guid}/#{FILENAME_WILDCARD}"
end
@key
end
啧。我只是直接在上传器中覆盖了store_dir和guid方法,而不是尝试覆盖这个方法:
def store_dir
"#{model.group.id}/photos"
end
def guid
Time.now.utc.strftime("%Y-%m-%d-%H%M%S")
end
希望这有助于某人。
答案 3 :(得分:-4)
https://github.com/gilcierweb/CMS-Rails
# app/uploads/galeria_imagem_uploader.rb
class GaleriaImagemUploader < CarrierWave::Uploader::Base
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}#{model.galeria_id}"
end
end
# app/models/adm/galeria_imagem.rb
class Adm::GaleriasImagem < ActiveRecord::Base
belongs_to :galeria, :polymorphic => true
validates :galeria, presence: true
# GaleriasImagem uploader using carrierwave
mount_uploader :imagem, GaleriaImagemUploader
end
# app/models/adm/galeria.rb
class Adm::Galeria < ActiveRecord::Base
has_many :galerias_imagens, :inverse_of => :galeria, :dependent => :destroy
# enable nested attributes for galerias_imagens through galeria class
accepts_nested_attributes_for :galerias_imagens, allow_destroy: true
end
# app/controllers/adm/galerias_controller.rb
class Adm::GaleriasController < ApplicationController
def create
@adm_galeria = Adm::Galeria.new(adm_galeria_params)
respond_to do |format|
if @adm_galeria.save
# to handle multiple images upload on create
if params[:adm_galeria][:imagem]
params[:adm_galeria][:imagem].each { |image|
@adm_galeria.galerias_imagens.create(imagem: image)}
end
format.html { redirect_to @adm_galeria, notice: 'Galeria was successfully created.' }
format.json { render :show, status: :created, location: @adm_galeria }
else
format.html { render :new }
format.json { render json: @adm_galeria.errors, status: :unprocessable_entity }
end
end
end
end
# app/controllers/adm/galerias_imagens_controller.rb
class Adm::GaleriasImagensController < ApplicationController
def create
@adm_galerias_imagem = Adm::GaleriasImagem.new(adm_galerias_imagem_params)
@adm_galerias_imagem.save
end
end