Using Carrierwave to upload Images,
I want to write 1 function to handle all my image includes in several sizes.
image_tag @photo_main.file.url(:img_122x145) rescue nil
The :img_120x120 is defined in Carrierwave uploader but why the :img_120x120 semicolon before its name? In what format is this?
Wanted outcome:
def get_avatar(size)
image_tag @photo_main.file.url(size) rescue nil
end
How could this be done?
UPDATE 1:
Fails with : ActionView::Template::Error (undefined method `file’ for nil:NilClass):
1: .ruler
2:
3: //= show_avatar_profile(@profile.id)
4: = show_avatar_new(@profile.id, “96×96”)
def show_avatar_new(id, size)
puts "size is"
size = size.to_sym
puts size
@photo_main = Photo.where(:attachable_id => id, :attachable_type => "Profile", :main => true, :moderated => true, :approved => true).first
@photo = Photo.where(:attachable_id => id, :attachable_type => "Profile", :moderated => true, :approved => true).first
if @photo_main
image_tag @photo_main.file.url(size)
else
image_tag @photo.file.url(size)
end
end
UPDATE 2:
class PhotoUploader < 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
version :img_48x48 do
process :resize_to_fill => [48, 48]
end
version :img_58x58 do
process :resize_to_fill => [58, 58]
end
version :img_75x75 do
process :resize_to_fill => [75, 75]
end
version :img_96x96 do
process :resize_to_fill => [96, 96]
end
# Used in search results,
version :img_122x145 do
process :resize_to_fill => [122, 145]
end
version :img_200x200 do
process :resize_to_fill => [200, 200]
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
def delete_empty_upstream_dirs
path = ::File.expand_path(store_dir, root)
Dir.delete(path) # fails if path not empty dir
path = ::File.expand_path(base_store_dir, root)
Dir.delete(path) # fails if path not empty dir
rescue SystemCallError
true # nothing, the dir is not empty
end
end
In Ruby, things beginning with colons
:(not semicolons;!) are symbols, which are essentially immutable strings.It seems like what you’ve written there is exactly what you need. If you’re wondering where to put it, you could put it in a helper
Please don’t use
rescue nilthere, though. What error are you trying to catch? It would be much better to explicitly avoid it rather than using exceptions as flow control.would be sufficient to avoid the problem of a
@photo_mainwithout afile, and is much more intention-revealing (and, in fact, more performant). Worst-case, you should still explicitly state what sort of error you’re expecting to getThis short (<3min) screencast makes an excellent case for avoiding inline rescue.
Update
When you create versions in CarrierWave, it creates methods to access them – you don’t pass an argument to
url:If you want to get a variable version, though, they are available through
versions(a hash):That won’t solve your remaining problem, which is simply that your queries aren’t finding anything.