ruby on rails - Rails4: How to validate image size uploaded by carrierwave? -
ruby on rails - Rails4: How to validate image size uploaded by carrierwave? -
i'm using rails4 , carrierwave upload files, , have working. have come check image size next aritcle, 2. model level validation.
i added validate :image_size_validation
in models\article.rb, nomethoderror in articlescontroller#create
appeared.
how can check image size?
should utilize file_size_validator.rb
(refer how to: validate attachment file size) instead of solution in above article?
any ideas? 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 less 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 ...
article table
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);
photos 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 saving image binary
development.log
started post "/articles" 127.0.0.1 @ 2014-10-08 19:47:05 +0900 processing articlescontroller#create 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".* "users" "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' #<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
in photo.rb instead of article.rb, no error message appeared when uploaded on 1mb 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 less xmb" if image.size > 1.megabytes #add next code instead of above line if image.size > 1.megabytes errors.add(:base, "image should less 1mb") end end end
\log\development.log
started post "/articles" 127.0.0.1 @ 2014-10-11 05:50:06 +0900 processing articlescontroller#create 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".* "users" "users"."remember_token" = 'xxxx' limit 1 [1m[36m (0.0ms)[0m [1mbegin transaction[0m [1m[35msql (3.0ms)[0m insert "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 info inserted `string` type on column `image` [1m[36msql (1.0ms)[0m [1minsert "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 http://localhost:3000/users/1 completed 302 found in 1306ms (activerecord: 7.0ms)
try in photo model
validate :image_size_validation, :if => "image?" def image_size_validation if image.size > 1.megabytes errors.add(:base, "image should less 1mb") end end
remove private
declaration
ruby-on-rails validation ruby-on-rails-4 carrierwave
Comments
Post a Comment