带CarrierWave的动态存储目录

时间:2012-01-07 22:55:39

标签: ruby-on-rails ruby upload carrierwave

只有Rails的新手,但我开始使用CarrierWave进行上传处理。我正在尝试制作一个媒体托管应用程序:

  • 节目属于用户
  • 一个节目有很多剧集
  • 一集属于视频

目前我正在使用单表继承将我的视频内容存储到元数据类型资产表中,所有这些都可以很好地保存。我想要解决的问题是当我通过CarrierWave保存视频时,我希望能够将show slug和episode slug放入保存路径。

目前我已将此设置与我的节目和剧集上传:

def store_dir
  "shows/#{model.friendly_id}/cover/"
end

之前在STI之前,我只是将文件名存储在剧集表的视频字段中,并通过以下方式存储:

def store_dir
  "shows/#{model.show.slug}/episodes/#{model.id} - #{model.friendly_id}"
end

现在只有问题我无法从CW上传器中的视频模型中获取剧集信息。

我的模特是:

Show.rb

class Show < ActiveRecord::Base

  #Associations
  belongs_to :user
  belongs_to :category
  has_many :episodes

  #Slugs
  extend FriendlyId
  friendly_id :title, use: :slugged

  #Carrierwave
  mount_uploader :image, ShowImageUploader

end

Episode.rb

class Episode < ActiveRecord::Base

  belongs_to :show
  belongs_to :image, :class_name => "Episode::Image"
  belongs_to :audio, :class_name => "Episode::Audio"
  belongs_to :video, :class_name => "Episode::Video"

  accepts_nested_attributes_for :video, :image, :audio

  before_save :add_guid, :on => :create

  #Slugs
  extend FriendlyId
  friendly_id :title, use: :slugged

  validates :title, :presence => true, :length => {:within => 5..40}

  protected

  def add_guid
    self.guid = UUIDTools::UUID.random_create.to_s
  end

end

剧集/ video.rb

class Episode::Video < Asset

  mount_uploader :video, EpisodeVideoUploader, :mount_on => :filename

end

asset.rb

class Asset < ActiveRecord::Base

end

所以我想如何在上传后获取相应的数据:

"shows/#{model.show.slug}/episodes/#{model.id} - #{model.friendly_id}"

谢谢!

0 个答案:

没有答案