0

please help solve the problem.

I use gem paperclip and documentation: https://github.com/thoughtbot/paperclip/tree/master#validations

I implemented upload files. it works. Now I need to add validation rules. I do the 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 a result of getting into the console the following error message:

Started POST "/videos" for 127.0.0.1 at 2015-07-22 17:18:22 +0300
Processing by VideosController#create as 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 with 204800 failed):
  app/controllers/videos_controller.rb:40:in `create'

on screen displays the following error message:

ArgumentError in VideosController#create
comparison of String with 204800 failed
stackov8
  • 414
  • 4
  • 16

2 Answers2

0

you can try this:

  validate :file_size_validation, :if => "video?"  

  def file_size_validation
    errors[:video] << "should be less than 2MB" if video.size > 2.megabytes
  end

For more information see this stackoverflow question.

Community
  • 1
  • 1
Mezbah
  • 1,247
  • 2
  • 17
  • 32
0
 validate :file_size_validation, :if => "video?"  

  def file_size_validation
    errors[:video] << "should be less than 2MB" if video.size.to_i > 1.kilobytes
  end  
stackov8
  • 414
  • 4
  • 16