Search code examples
excelruby-on-rails-4paperclippaperclip-validation

paperclip content type for xls and xlsx


struggling with paperclip content type, need to upload xls/xlsx file.

has_attached_file :sheet
validates_attachment_content_type :sheet, content_type: [
                                          'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                                          'application/zip',
                                          'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                                          'application/vnd.ms-excel',
                                          'application/xls',
                                          'application/xlsx',
                                          'application/octet-stream'
                                        ],
                                message: ' Only EXCEL files are allowed.' 

NOTE: Created a sheet from google drive.

Above content types i tried but every time got the same error

Output

Command :: file -b --mime '/var/folders/zy/khy_wsfn7jbd40bsdps7qwqc0000gt/T/5a76e813d6a0a40548b91acc11557bd220160328-13642-1meqjap.xlsx'
   (0.2ms)  BEGIN
Command :: file -b --mime '/var/folders/zy/khy_wsfn7jbd40bsdps7qwqc0000gt/T/5a76e813d6a0a40548b91acc11557bd220160328-13642-114d8t6.xlsx'
   (0.3ms)  ROLLBACK
{:sheet_content_type=>[" Only EXCEL files are allowed."], :sheet=>[" Only EXCEL files are allowed."]}

Solution

  • Missed the path ; (

    Fixed it by using

    has_attached_file :sheet,
                    :path => ":rails_root/public/system/:attachment/:id/:filename"
    
    validates_attachment :sheet, presence: true,
                       content_type: { content_type: [
                         "application/vnd.ms-excel",
                         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                       ]
                       },
                       message: ' Only EXCEL files are allowed.'