Rails4: How to check the size of the image loaded by the carrier?

I am using Rails4 and Carrierwave to upload files and I have a job to do. I came to check the size of the image with the following this aritcle, 2. Checking the model level .

I added validate :image_size_validation

to models \ article.rb but it showed up NoMethodError in ArticlesController#create

.

How do I check the size of an image?

Should I use file_size_validator.rb

(see How to: Check attachment file size ) instead of the solution in the above article?

Any ideas? Thanks in advance.

\ models \ article.rb

class Article < ActiveRecord::Base
...
    has_many :photos, dependent: :destroy
    validate :image_size_validation
...
    private

    def image_size_validation
        errors[:image] << "should be less than 1MB" if photos.image.size > 1.megabytes
    end

end

      

\ models \ photo.rb

class Photo < ActiveRecord::Base
    belongs_to :article
    mount_uploader :image, ImageUploader
end

      

\ Controllers \ article_controller.rb

...
  def create
    @article = current_user.articles.build(article_params)
    if @article.save
      flash[:success] = "article created!"
      redirect_to current_user #root_url
    else
        @article.build_images
       render 'new'
    end
  end
...

      

table of articles

sqlite> .schema articles
CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "content" varchar(255),"user_id" integer, "created_at" datetime, "updated_at" datetime, "category_id" integer);

      

photo table

sqlite> .schema photos
CREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "article_id" integer,"image" varchar(255), "created_at" datetime, "updated_at" datetime, "bin_image" blob); #prepare bin_image for saving image as binary 

      

development.log

Started POST "/articles" for 127.0.0.1 at 2014-10-08 19:47:05 +0900
Processing by ArticlesController#create as HTML
  Parameters: {"utf8"=>"笨・, "authenticity_token"=>"xxx", "article"=>{"category_id"=>"1718", "photos_attributes"=>{"0"=>{"article_id"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x4579140 @tempfile=#<File:C:/xxx/AppData/Local/Temp/RackMultipart20141008-5252-vsv6z>, @original_filename="DSCN0721_080.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"article[photos_attributes][0][image]\"; filename=\"DSCN0721_080.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "1"=>{"article_id"=>""}, "2"=>{"article_id"=>""}}, "content"=>"test"}, "commit"=>"逋サ骭イ縺吶k"}
  [1m[35mUser Load (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'xxx' LIMIT 1
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35m (0.0ms)[0m  rollback transaction
Completed 500 Internal Server Error in 888ms

NoMethodError (undefined method `image' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Photo:0x472f8d8>):
  app/models/article.rb:27:in `image_size_validation'
  app/controllers/articles_controller.rb:20:in `create'

      

Edit

I added image_size_validation

to photo.rb instead of article.rb but no error message came up when uploading more than 1MB of image.

\ models \ photo.rb

class Photo < ActiveRecord::Base
    belongs_to :article
    mount_uploader :image, ImageUploader

validate :image_size_validation

    private

    def image_size_validation
        #errors[:image] << "should be less than xMB" if image.size > 1.megabytes
        #add the following code instead of above line
      if image.size > 1.megabytes
        errors.add(:base, "Image should be less than 1MB")
      end

    end

end

      

\ LOG \ development.log

Started POST "/articles" for 127.0.0.1 at 2014-10-11 05:50:06 +0900
Processing by ArticlesController#create as HTML
  Parameters: {"utf8"=>"笨・, "authenticity_token"=>"xxxx", "article"=>{"categort_id"=>"1718", "photos_attributes"=>{"0"=>{"article_id"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x43af760 @tempfile=#<File:C:/xxxx/AppData/Local/Temp/xxxxk>, @original_filename="DSCN0721.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"article[photos_attributes][0][image]\"; filename=\"DSCN0721.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "1"=>{"article_id"=>""}, "2"=>{"article_id"=>""}}, "content"=>"test"}, "commit"=>"逋サ骭イ縺吶k"}
  [1m[35mUser Load (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'xxxx' LIMIT 1
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35mSQL (3.0ms)[0m  INSERT INTO "articles" ("content", "created_at", "category_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["content", "test"], ["created_at", Fri, 10 Oct 2014 20:50:08 UTC +00:00], ["category_id", 1718], ["updated_at", Fri, 10 Oct 2014 20:50:08 UTC +00:00], ["user_id", 1]]
Binary data inserted for `string` type on column `image`
  [1m[36mSQL (1.0ms)[0m  [1mINSERT INTO "photos" ("article_id", "created_at", "image", "updated_at") VALUES (?, ?, ?, ?)[0m  [["article_id", 78], ["created_at", Fri, 10 Oct 2014 20:50:08 UTC +00:00], ["image", "xxxx.jpg"], ["updated_at", Fri, 10 Oct 2014 20:50:08 UTC +00:00]]
  [1m[35m (3.0ms)[0m  commit transaction
Redirected to http://localhost:3000/users/1
Completed 302 Found in 1306ms (ActiveRecord: 7.0ms)

      

+3


source to share


2 answers


try this in your fashion model

validate :image_size_validation, :if => "image?"

def image_size_validation
    if image.size > 1.megabytes
      errors.add(:base, "Image should be less than 1MB")
    end
 end

      



remove ad private

+6


source


Since the 1.0

CarrierWave version has a built in file size checker.

Install the last carrierwave

gem

gem 'carrierwave', '~> 1.0'

      

Add a method size_range

to enforce minimum size and maximum size

class ImageUploader < CarrierWave::Uploader::Base
  def size_range
    0..2.megabytes
  end

      

In the model, add validates_integrity_of

to confirm the file size (and content type) of the image.



class Image < ApplicationRecord
  mount_uploader :image, ImageUploader

  validates_integrity_of :image

      

Rspec specification:

it 'is invalid with an image size greater 2mb' do
  image = build(:image, image: File.open(File.join(Rails.root, 'spec/support/images/image-3mb.jpg')))
  image.valid?
  expect(image.errors[:image]).to include("File size should be less than 2097152")
end

      

To make the error message friendlier, override the carrierwave method check_size!

in ImageUploader

.

 private
 def check_size!(new_file)
    size = new_file.size
    expected_size_range = size_range
    if expected_size_range.is_a?(::Range)
      if size < expected_size_range.min
        raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.min_size_error", :min_size => ApplicationController.helpers.number_to_human_size(expected_size_range.min))
      elsif size > expected_size_range.max
        raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.max_size_error", :max_size => ApplicationController.helpers.number_to_human_size(expected_size_range.max))
      end
    end
  end

      

+4


source







All Articles