Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails carrierwave default_url not working

i don't think im understanding this correctly, but how do i give users a default image?

   def default_url
      "/images/fallback/" + [version_name, "default.png"].compact.join('_')
   end

i have an image called 'default.png' inside my images/fallback directory. ive seen online also people change the version_name to something like 'tiny' but that doesn't seem to work for me either. what's going on? what exactly is the version name?

i tried something like

   def default_url
      "/images/fallback/default.png"
   end

but that doesn't work either. what am i misunderstanding? thanks!

edit:

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick

  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
      ???????
   end

  # Create different versions of your uploaded files:
   version :thumb do
     process :resize_to_fill => [80, 80]
   end

end





class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation,
     :remember_me, :image, :remote_image_url, :image_cache

  mount_uploader :image, ImageUploader
end
like image 463
Sasha Avatar asked Apr 11 '12 05:04

Sasha


1 Answers

If you are using the asset pipeline then you do not need to include "images" in your file path because the image_tag will automatically route to the assets/images folder and then look for the file name there. So the path you need in the default_url method is whatever comes after the images folder. In my case it was just default.png because I had no sub folders in the images folder.

    def default_url
       'default.png'
    end
like image 131
rocket scientist Avatar answered Nov 05 '22 18:11

rocket scientist