正确的Carrierwave image_tag路径上的娱乐

时间:2012-02-02 20:40:46

标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.1 carrierwave

我无法找到包含Carrierwave图像的正确路径。 经过多次搜索和尝试所有可能的组合后,我希望有人知道:

我有

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

1 个答案:

答案 0 :(得分:1)

你试过这样的事吗?

= image_tag current_user.profiles.assets.file_url(:search) rescue nil