通过回形针上传文件时Rails未知格式

时间:2016-02-22 07:37:24

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

我正在使用paperclip gem处理rails并尝试上传文档,

这是我的html表单

<div style="background: #ee6e73; width: 100%;"><h3 class="center-align" style="color: #fff;font-weight: 600 !important;">Upload Resume</h3></div>
<div class="modal-content">
  <%= form_for(Document.new, :url => resume_upload_individual_profile_path, remote: true, :html => {:multipart => true } ) do |f| %>
      <%= hidden_field_tag :authenticity_token, form_authenticity_token %>
      <%= hidden_field_tag :profile_id, @profile.id %>
      <div class="row">

        <div class="file-field input-field">
          <div class="btn">
            <span>Choose Document</span>
            <i class="material-icons prefix">input</i>
            <%= file_field_tag :document %>
          </div>
          <div class="file-path-wrapper">
            <input class="file-path validate" type="text">
          </div>
        </div>

      </div>

      </div>
      <div class="modal-footer">
        <button type="submit" style="float: left;margin-top: 0%;" class="waves-effect waves-light btn-large  "><i class="material-icons left">cloud</i>Submit</button>
        <a href="#!" class=" modal-action modal-close waves-effect waves-green btn-flat">Close</a>
      </div>
  <% end %>

我的控制器操作是

def resume_upload

    p params
    @resume = Document.create(resume_params)
    if @resume.save
      @message = 'Your Resume has been successfully uploaded'
      # redirect_to individual_profile_path(individual_id: current_individual.id)
    else

      # redirect_to individual_profile_path(individual_id: current_individual.id)
      @message = find_error(@resume)
    end
    respond_to do |format|
      format.js
    end

  end

我的模特有

has_attached_file :document
  validates_presence_of :document
  validates_attachment_content_type :document, :content_type => ["application/pdf","application/vnd.ms-excel",
                                                                 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                                                 "application/msword",
                                                                 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                                                                 "text/plain"] 

我面临的问题是,当我选择文件并尝试上传时,我收到错误ActionController::UnknownFormat

但是当我在没有选择文件的情况下提交表单时,我没有遇到这个问题,我能够成功进入js.erb文件。

this is the console error

1 个答案:

答案 0 :(得分:0)

为了使它能够使用这些文件,您需要将mime类型添加到服务器配置中。在Rails中,这是在config / initializers / mime_types.rb

中完成的
Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :docx

这将有助于: - What is a correct mime type for docx, pptx etc?