2

我想为 Carrier wave Direct 配置上传路径,我用它来将文件直接上传到 Amazon s3。在 app/uploaders/myuploader.rb 中,我有,

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

我想修改它,使路径看起来像
“uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{User.name}”。
所以它会像
“uploads/Users/avatar/michael_jordan/..”这样的东西,
其中 User 是安装上传器的模型。

我已经在 github ( https://github.com/dwilkie/carrierwave_direct ) 中浏览了 Carrier wave Gem 的源代码,但我不知道如何将值从活动模型对象传递给上传者以创建上传路径(亚马逊 s3 的密钥)。有没有办法做到这一点?

谢谢!

4

4 回答 4

5

好的。一个月后我被迫解决同样的问题,这次我解决了。在这里发布我的解决方案,以便它可以帮助遇到同样问题的其他人,1)覆盖 store_dir 使其返回一个空字符串,

def store_dir
 ""
end

2)到达您的控制器并将您的首选键分配给您的上传器对象的“键”属性,例如,

  @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.

这里 player 是模型对象,avatar 是安装上传器的属性,键将设置为播放器名/文件名。

这就是我要找的:)

于 2012-11-15T20:24:44.257 回答
2

如果model.name不起作用,您可以尝试使用#read_attribute方法直接访问名称字段

所以你的 store_dir 方法看起来像

def store_dir
  uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.send(:name)}
end

但我相信model.name应该工作

于 2012-10-08T21:19:39.237 回答
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

希望这可以帮助某人。

于 2014-01-06T04:41:14.303 回答
-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
于 2014-07-26T21:06:52.367 回答