0
我無法找到包含我的載波圖像的正確路徑。 許多搜索和嘗試所有可能的組合,我希望以後有人知道這裏:在正確的載體上的娛樂image_tag路徑
我
asset.rb
belongs_to :attachable,
:polymorphic => true
mount_uploader :file, AssetUploader
profile.rb
has_many :assets, :as => :attachable
在我的看法我用
= image_tag @profile.assets.first.file_url(:search) rescue nil
但這包括上傳的第一個圖像。
說一個_profile.haml部分我想添加屬於該current_user的圖像如何做到這一點?
My uploader
class AssetUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
process :resize_to_fill => [600, 600]
version :profile do
process :resize_to_fill => [300, 300]
end
version :search do
process :resize_to_fill => [124, 124]
end
version :thumb do
process :resize_to_fill => [96, 96]
end
version :small do
process :resize_to_fill => [48, 48]
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
"#{secure_token(10)}.#{file.extension}" if original_filename.present?
end
def default_url
"/images/" + [version_name, "avatar_default.jpg"].compact.join('_')
end
protected
def secure_token(length=32)
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2))
end
end
具有相同的問題。它工作,除非沒有資產,我沒有得到正確的網址。你有沒有找到解決這個問題的方法。我現在有同樣的問題! – bokor 2012-04-25 20:40:14