0

CarriveWave の store_dir に関して誰か助けてください。

関連する属するモデルのパーマリンクに基づいてファイルを保存する複数の画像モデルをどのように作成しますか?

# Garage model
class Garage < ActiveRecord:Base

  attr_accessible: :avatar, :permalink, :car_image_attributes,
                   :motorcycle_image_attributes

  has_many :car_image
  has_many :motorcycle_image

  mount_uploader :avatar, AvatarUploader

  def set_permalink
    self.permalink = permalink.parameterize
  end

  def to_param
    permalink.parameterize
  end

end

これは、CarrierWave とリンクする私のイメージ モデルです。

# Car Image model
CarImage < ActiveRecord:Base
  belongs_to :garage
  attr_accessible: :garage_id, :image

  mount_uploader :car_image, CarUploader

end

# Motocycle Image model
MotocycleImage < ActiveRecord:Base
  belongs_to :garage
  attr_accessible: :garage_id, :image

  mount_uploader :motorcycle_image, MotocycleUploader

end

これは私の CarrierWave アップローダがどのように見えるかです。

# CarrierWave avatar uploader
avatar_uploader.rb
  # This uploader directly relates to the Garage model table 
  # column avatar:string.

  def store_dir
    # This works for the avatar because it calls on the Garage permalink
    # but it fails for the other image models because it's a model relation
    # has_many, belongs_to and the model.permalink will be based on the
    # uploader's respective model and not the Garage model
    # eg. car_uploader.rb = model.permalink = CarImage.permalink
    # I would like it to refer to to Garage.permalink at all times.
    "garage/#{model.permalink}/#{mounted_as}/"
  end
end

# CarrierWave car and motorcycle uploaders
car_uploader.rb
# Fails to upload because it doesn't know what permalink is
end

motorcycle_uploader.rb
# Fails to upload because it doesn't know what permalink is
end

明確にしたい場合は申し訳ありませんが、与えられた洞察に感謝します。

4

1 に答える 1

2

おそらく最も簡単な方法は、パーマリンクをモデルの親に委任することです

CarImage < ActiveRecord:Base
  belongs_to :garage
  delegate : permalink, :to => :garage
  attr_accessible: :garage_id, :image

  mount_uploader :car_image, CarUploader



end
于 2012-10-10T17:12:58.123 に答える