2014-02-20 175 views
3

我們的代碼看起來像磨回形針運行回形針4.1時沒有validates_attachment_file_name:升級到3.5

has_merchants_attached_file :pdf, 
    storage:   :s3, 
    s3_credentials: Mbc::DataStore.s3_credentials, 
    s3_permissions: :private, 
    path:    ":identifier_template.pdf", 
    bucket:   Mbc::DataStore.forms_and_templates_bucket_name 


    validates_attachment_file_name :pdf, :matches => [/pdf\Z/] 

產生一個錯誤:有趣的是

undefined method `validates_attachment_file_name' for #<Class:0x007fba67d25fe0> 

,當我們倒等級回到3.5,我們遇到同樣的問題。

正在生成這個控制器是:

def index 
    @fidelity_templates = FidelityTemplate.order("identifier asc").all 
end 

此外:

def has_merchants_attached_file(attribute, options={}) 
    if Rails.env.test? || Rails.env.development? 
    has_attached_file attribute, 
    path: "paperclip_attachments/#{options[:path]}" 
    else 
    has_attached_file attribute, options 
    end 
end 

什麼可能會導致這有什麼想法?

+0

「has_merchants_attached_file」是什麼意思?這是你自己的方法嗎?如果是,請分享一下。 –

+0

@KirtiThorat。不,我沒有。它來自下面提到的文檔。 –

+0

你指的是哪些文件?你能指出一下嗎? –

回答

2

您可以瞭解所提供的驗證此:

https://github.com/thoughtbot/paperclip#validations

所包含的驗證程序是:

  • AttachmentContentTypeValidator
  • AttachmentPresenceValidator
  • AttachmentSizeValidator

它們可以通過以下兩種方式使用:

# New style: 
validates_with AttachmentPresenceValidator, :attributes => :avatar 

# Old style: 
validates_attachment_presence :avatar 

UPDATE ...

如果進一步往下看了我上面給你的鏈接會得到關於安全驗證的一部分(感謝基爾提Thorat):

https://github.com/thoughtbot/paperclip#security-validations

他們就如何驗證文件名格式的示例:

# Validate filename 
validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/] 

從您的代碼片段看起來您的驗證應該按原樣運行。

但是,我從來沒有見過這種語法使用回形針:

has_merchants_attached_file ... 

也許這就是你的問題的根源?您通常會使用以下命令將文件附加到您的模型中:

has_attached_file :pdf ... 
+2

檢查Paperclip gitrepo中「驗證部分」下的「安全驗證」部分。 OP正在努力做到這一點。 –