Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:40:32+00:00 2026-06-17T14:40:32+00:00

Using Carrierwave to upload Images, I want to write 1 function to handle all

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:40:33+00:00Added an answer on June 17, 2026 at 2:40 pm

    In Ruby, things beginning with colons : (not semicolons ;!) are symbols, which are essentially immutable strings.

    "img_122x145".to_sym # => :img_122x145
    

    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

    # app/helpers/avatar_helper.rb
    def get_avatar(size)
      image_tag @photo_main.file.url(size)
    end
    

    Please don’t use rescue nil there, though. What error are you trying to catch? It would be much better to explicitly avoid it rather than using exceptions as flow control.

    image_tag @photo_main.file.url(size) if @photo_main.file?
    

    would be sufficient to avoid the problem of a @photo_main without a file, 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 get

    def get_avatar(size)
      image_tag @photo_main.file.url(size)
    rescue SomeSpecificErrorThatCantBeAvoided
      nil
    end
    

    This 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:

    @photo.file.img_122x145.url
    

    If you want to get a variable version, though, they are available through versions (a hash):

    size = :img_122x145
    @photo.file.versions[size].url
    

    That won’t solve your remaining problem, which is simply that your queries aren’t finding anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using Rails 3.2.6 and using carrierwave to upload images.When i upload image it
I am using carrierwave to upload images to amazon s3. This works great on
I am using the Carrierwave plugin to upload images. It works fine, but if
I'm using Carrierwave in my Rails app to handle image uploads. When I configure
I'm using carrierwave to upload image files. I basically have a form field _form.html.erb
Im trying to upload images to S3 on Ruby on Rails using carrierwave and
I'm using rails 3.2 with asset and carrierwave for upload some images, they store
I'm using carrierwave to allow users to upload images with an ability to crop
everyone! I was following Railscasts episode #253 , using CarrierWave to do image upload.
I'm using CarrierWave in a Rails3 app to upload assets. All good on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.