【问题标题】:Upload code files with paperclip使用回形针上传代码文件
【发布时间】:2016-03-09 15:01:18
【问题描述】:

validates_attachment_content_type 配置应该是什么才能上传代码文件。就我而言,我想上传 .R 文件。

我想做这样的事情:

class RFile < ActiveRecord::Base
  has_attached_file :r, url: "/system/:attachment/:id/:basename.:extension"
  validates_attachment_content_type :r, content_type: 'text/r'
end

我必须定义 MIME 类型吗?我该怎么做?

编辑:

有了这段代码,使用text/plain:

class RFile < ActiveRecord::Base
  has_attached_file :r, url: "/system/:attachment/:id/:basename.:extension"
  validates_attachment_content_type :r, content_type: 'text/plain'
end

我收到以下错误:

R has contents that are not what they are reported to be
R is invalid
R content type is invalid

我查看了这个 mime 类型列表

http://hul.harvard.edu/ois/systems/wax/wax-public-help/mimetypes.htm

但我找不到 .R 文件的那个。但是在执行这个命令时:

file --mime-type compare_datasets.R 

我得到这个结果:

compare_datasets.R: text/plain

为什么text/plain 不起作用?

【问题讨论】:

    标签: ruby-on-rails r ruby-on-rails-4 paperclip paperclip-validation


    【解决方案1】:

    对于代码文件,您可以使用text/plain MIME 类型。

    validates_attachment_content_type :r, :content_type => 'text/plain'
    

    可以在here找到许多这样的文件结尾

    【讨论】:

    • 我尝试使用 text/plain 但我仍然无法上传文件。我用代码和错误编辑了问题。
    • 试试这个text/x-rmime 类型
    【解决方案2】:

    我设法通过在 mime 类型初始化程序中执行此操作来使其工作:

    Paperclip.options[:content_type_mappings] = {r: "text/plain"}
    

    模型中的这个:

    class RFile < ActiveRecord::Base
      has_attached_file :r, url: "/system/:attachment/:id/:basename.:extension"
      validates_attachment_file_name :r, matches: [/r\Z/, /R\Z/]
      do_not_validate_attachment_file_type :r
    end
    

    【讨论】:

      猜你喜欢
      • 2014-05-23
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多