Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 Rails回形针内容类型验证(按排除)_Ruby On Rails 4_Paperclip - Fatal编程技术网

Ruby on rails 4 Rails回形针内容类型验证(按排除)

Ruby on rails 4 Rails回形针内容类型验证(按排除),ruby-on-rails-4,paperclip,Ruby On Rails 4,Paperclip,我正在开发一个内部使用的消息系统,每条消息都可以有附件。所说的附件几乎可以是任何类型的文件,但有一些类型的文件我不想允许,例如前男友 与其在模型验证中列出大量已接受的内容类型(这远远不实际,而且肯定会阻止应接受的文件),我宁愿简单地指定应而不是应接受的内容类型 谷歌搜索没有产生任何结果,但我相信其他人以前也遇到过这种情况,你知道如何最好地解决这个问题吗?所以我最后就是这么做的: has_mongoid_attached_file :file do_not_validate_attachment

我正在开发一个内部使用的消息系统,每条消息都可以有附件。所说的附件几乎可以是任何类型的文件,但有一些类型的文件我不想允许,例如前男友

与其在模型验证中列出大量已接受的内容类型(这远远不实际,而且肯定会阻止应接受的文件),我宁愿简单地指定应而不是应接受的内容类型


谷歌搜索没有产生任何结果,但我相信其他人以前也遇到过这种情况,你知道如何最好地解决这个问题吗?

所以我最后就是这么做的:

has_mongoid_attached_file :file

do_not_validate_attachment_file_type :file
validate :excluded_content_type

def excluded_content_type
  forbidden = [
    "application/x-msdownload",
    "application/x-ms-installer",
    "application/x-elf",
    "application/x-sh",
    "application/x-shellscript",
    "text/x-perl",
    "text/x-python",
    "application/javascript"
  ]
  errors.add(:base, 'Executable and script files are not allowed.') if forbidden.include? file_content_type
end

我不知道这是否是最好的方法,但它是有效的。如果你有更好的方法,就把你的答案贴出来

如果内容类型不应被接受

在model.rb中写入

  has_attached_file :record_attachment      
  validates_attachment_content_type :record_attachment, presence: false, :content_type => {:not => "application/zip"}

请编辑更多信息。不鼓励只编写代码和“试试这个”答案,因为它们不包含可搜索的内容,也不解释为什么有人应该“试试这个”。