我是 Ruby on Rails(rails 3)的新手。我有一个包含三个字段的表单,在这三个字段中,一个字段是上传文件(rar/tar/zip)。我想知道验证(服务器端)这种允许用户的表单的方法:-
1. Fill in all three fields.
2. Uploaded file should be either rar/tar/zip.
3. Uploaded file should not exceed 2MB in size.
我是 Ruby on Rails(rails 3)的新手。我有一个包含三个字段的表单,在这三个字段中,一个字段是上传文件(rar/tar/zip)。我想知道验证(服务器端)这种允许用户的表单的方法:-
1. Fill in all three fields.
2. Uploaded file should be either rar/tar/zip.
3. Uploaded file should not exceed 2MB in size.
您可以使用回形针 gem 上传文件。
class User < ActiveRecord::Base
attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
有类似的验证
validates_attachment :avatar, :presence => true,
:content_type => { :content_type => "image/jpg" },
:size => { :in => 0..10.kilobytes}
有关更多信息,请参阅https://github.com/thoughtbot/paperclip。
您可以使用此代码,要使用此代码,您必须使用回形针 gem
validates_attachment_size :upload, :less_than => 2.megabytes,
:with => %r{\.(rar|tar|zip)$}i