ruby on rails - how to validate the filsize? -


please solve problem.

i use gem paperclip , documentation: https://github.com/thoughtbot/paperclip/tree/master#validations

i implemented upload files. works. need add validation rules. following: model:

class video < activerecord::base   validates :title, presence: true   validates :video, presence: true    belongs_to :user    has_attached_file :video, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"  validates_attachment :video, :presence => true,   :content_type => { :content_type => "image/jpeg" },   :size => { :in => 0..200.kilobytes }   end 

controller:

def create    @video = video.new(video_params)       if @video.save     flash[:success] = :video_created     redirect_to @video   else     flash.now[:error] = :user_not_created     render 'new'   end end  def video_params   params.require(:video).permit(:title, :video) end   

as result of getting console following error message:

started post "/videos" 127.0.0.1 @ 2015-07-22 17:18:22 +0300 processing videoscontroller#create html   parameters: {"utf8"=>"✓", "authenticity_token"=>"wipqdmh1irqkjgyctpfl/j0uvkgvv4ptml9l1xhyfaa0q/wp/jcofllwf4nr3wktjub+b7tvbpg5xfybp/oa9q==", "video"=>{"title"=>"yyy", "video"=>#<actiondispatch::http::uploadedfile:0x007f140184ff00 @tempfile=#<tempfile:/tmp/rackmultipart20150722-29111-yn1mqv.jpg>, @original_filename="btwzqhonwrs.jpg", @content_type="image/jpeg", @headers="content-disposition: form-data; name=\"video[video]\"; filename=\"btwzqhonwrs.jpg\"\r\ncontent-type: image/jpeg\r\n">}, "commit"=>"create video"} command :: file -b --mime '/tmp/8924685e4bfe6957f64963710295779820150722-29111-wl5fmt.jpg' completed 500 internal server error in 11ms (activerecord: 0.0ms)  argumenterror (comparison of string 204800 failed):   app/controllers/videos_controller.rb:40:in `create' 

on screen displays following error message:

argumenterror in videoscontroller#create comparison of string 204800 failed 

you can try this:

  validate :file_size_validation, :if => "video?"      def file_size_validation     errors[:video] << "should less 2mb" if video.size > 2.megabytes   end 

for more information see stackoverflow question.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -