【问题标题】:Rails unknown format when file is uploaded by paperclip通过回形针上传文件时Rails未知格式
【发布时间】:2016-02-22 07:37:24
【问题描述】:

我正在使用回形针宝石在轨道上工作并尝试上传文档,

这是我的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 文件。

【问题讨论】:

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


    【解决方案1】:

    为了让它与这些文件一起工作,您需要将 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?

    【讨论】:

    • 那或许你应该多发些日志来帮助我们更好地理解。
    • Rails 有几个内置的 Mime:Type。通过查看您的日志,这显然是 mime 类型的问题,您的文件名显示为 card.png,但您没有验证图像附件:- validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ 。如果它解决了您的查询,则将答案标记为已接受。
    • 但我不想上传 .png。我只希望上传 pdf 和文档。我尝试上传该 .png 文件,以便获得验证错误消息
    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多