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

  • SEARCH
  • Home
  • 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 6325885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:58:41+00:00 2026-05-24T16:58:41+00:00

I am using Ruby on Rails 3.0.9 and Paperclip 2.3. Since the Paperclip gem

  • 0

I am using Ruby on Rails 3.0.9 and Paperclip 2.3. Since the Paperclip gem offers only two validation methods (validates_attachment_presence and validates_attachment_content_type) I am trying to implement my custom validation methods.

In my model file I have just the following validation method

def validates_avatar(attribute_name, file)
  if file.nil? # file value is nil if no file is uploaded
    self.errors.add( "#{attribute_name}", "You must select a file" )
  else
    self.errors.add( "#{attribute_name}", "Avatar is an invalid image format") unless MIME_TYPES.include?(file.content_type)
    self.errors.add( "#{attribute_name}", "Avatar is too big" if ( ( file.size > AVATAR_FILE_MAX_SIZE.to_i ) || ( file.size == nil ) )
  end

  return self.errors.empty?
end

that I call from my controllers in this way:

if @user.validates_avatar(:avatar, params[:user][:avatar])
  ...
end

I would like to make the above validation to run\to trigger the same way of all others Ruby on Rails validation methods (eg: as-like validates :title, :presence => true works).

How can I do that and how can I improve the above code in order to handle avatar validations?

  • 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-05-24T16:58:42+00:00Added an answer on May 24, 2026 at 4:58 pm

    It is already included in Paperclip and it do just the same job. so why do you want to repet it?

    class Avatar < ActiveRecord::Base
      has_attached_file :file
      validates_attachment_presence :file
      validates_attachment_size :file, :less_than => 5.megabytes
      validates_attachment_content_type :file, :content_type => ['image/jpeg', 'image/png']
    end
    

    and never validate in Controller – it is Model job. Just

    @user = User.new(params[:user])
    @user.save
    

    It won’t save @user if @user.avatar won’t pass validation

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

Sidebar

Related Questions

I'm using Ruby 1.9 and Rails 2.3.4 with Paperclip gem to save attachments. and
I'm using: Paperclip 2.3.16 Rails 3.0.9 Ruby 1.9.2 AWS - S3 0.6.2 I'm trying
Using rails 3.1.1, Ruby 1.9.2, Gems: gem 'haml', gem 'simple_form', gem 'aws-sdk', gem 'paperclip',
using Ruby on Rails 2.3.2, since I already created Scaffold for Story, so instead
I am using Ruby on Rails 3.2.2 and Cucumber with the cucumber-rails gem. I
I'm using Paperclip with a Ruby on Rails to attach assets to a model,
I am trying to build a free web application using ruby/rails It should be
When I try to upload a photo in Ruby on Rails using Paperclip on
I'm using ruby 1.9.2, rails 3.0.8, paperclip 2.3.11. I installed imagemagick some time ago
I am using the carrierwave gem (http://github.com/jnicklas/carrierwave) for ruby on rails. How do I

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.