如何在轨道上的ruby中附加excel文件?

时间:2017-11-28 11:38:04

标签: ruby-on-rails excel

我的表单中有上传功能,可以上传PDF文件和文档。

当我尝试上传excel文件时,它会返回错误文件无效

如何上传Excel文件?

HTML页面(表单) -

<div class="box box-default">
  <div class="box-header with-border">
    <h3 class="box-title">HRMS Data Formate </h3>
  </div>
   <div class="box-body">
    <%= bootstrap_form_for(@hrms_data_formate,html: {id: 'hrms_data_formate'}) do |f| %>
    <div class="row">
      <div class="col-sm-6">
        <div class="form-group required">       
         <%= f.text_field :name ,label: "Name"%>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="form-group required">
         <%= f.file_field :document, label: 'Upload Document' %>
        </div>
      </div>
      <div class="col-sm-3">
        <label>Status</label>
        <div class="field">
            <%= f.check_box :status , label:'Active'%>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-6">
        <div class="field">
          <%= f.text_area :description ,label: "Description" %>
        </div>
      </div>
      <div class="col-sm-6">
        <div class="actions" style="padding: 30px">
          <%= f.submit 'Create',class: "btn btn-primary btn-sm"  %>
        </div>
      </div>
    </div>
   </div>
</div>
<% end %>

<script>
$("#hrms_data_formate").validate({
    rules: {
      "hrms_data_formate[name]":{
        required: true,
      },
      "hrms_data_formate[document]":{
        required: true,
      },
       },
    messages: {
      "hrms_data_formate[name]":{
        required: "Please specify name",
      },
       "hrms_data_formate[document]":{
        required: "Please upload document",
      },
       },
    errorPlacement: function(error, element) {
     error.css({"color": "red", "font-size": "12px","font-weight" : "normal"})
     error.insertAfter(element.parent(element));
    }
   });

</script>

型号代码 -

   has_attached_file :document, :path => "attachments/attach_resume/:basename.:extension"
   validates_attachment_presence :document
   validates_attachment_size :document, less_than: 15.megabytes
   validates_attachment :document, content_type: { content_type: %w(application/pdf application/msword application/msexcel application/xls application/vnd.openxmlformats-officedocument.wordprocessingml.document) }

请帮帮我,我无法上传excel文件。谢谢!

1 个答案:

答案 0 :(得分:0)

你可以试试吗

     <%= f.file_field :document, html: {name: "..."}, :accept => '.doc,.docx,.pdf,.png,.xls,.xlsx,.DOC,.DOCX,.PDF,.PNG,.XLS,.XLSX' %>

并删除

 validates_attachment :document, content_type: { content_type: %w(application/pdf application/msword application/msexcel application/xls application/vnd.openxmlformats-officedocument.wordprocessingml.document) }